Cannot specify `__forceinline` for an implementation of a member function in a separate translation unit

7 hours ago 1
ARTICLE AD BOX

Consider the following three files "main.cpp"

#include ".hpp" __declspec(noinline) int main(int, char**) { x().DoFoo(); x::dobar(); ::dobaz(); return 0; }

".hpp"

#pragma once struct x { void DoFoo(void); static void dobar(void); }; void dobaz(void);

and ".cpp"

#include ".hpp" #include <iostream> void x::DoFoo(void) { std::cout << "Hello!\n"; } void x::dobar(void) { std::cout << "World!\n"; } void dobaz(void) { std::cout << "!\n"; }

For /GL, when /Ob2 is specified, a function implemented in a single translation unit using __forceinline extern can allow the __forceinline hint to be applied to these functions. However, this is only possible with free functions because of syntax rules:

#include ".hpp" #include <iostream> /* * member function */ #if 0 /* * emits C4630 and causes C4716 */ extern void x::DoFoo(void) #elif 0 /* * wont emit an external implementation for x::DoFoo */ __forceinline void x::DoFoo(void) #else /* * OK */ void x::DoFoo(void) #endif { std::cout << "Hello!\n"; } /* * static member function * emits C4630 but still seems to compile? not really sure whats going on here */ __forceinline extern void x::dobar(void) { std::cout << "World!\n"; } /* * free function * compiles */ __forceinline extern void dobaz(void) { std::cout << "!\n"; }

My main question is how do I get the benefits of __forceinline (or similar) for member functions if possible? Otherwise what facilities should I try outside of __forceinline?

Read Entire Article