c++ - Forward declaring a constexpr function in a header -


this question has answer here:

say have following files. invalid c++ (linker chokes, yeah) or mistake in syntax? must forward declaration of constexpr function in same file definition?

header.h

extern constexpr int fun(int); 

source.cpp

constexpr int fun(int x)  {     return x * 2;  } 

it's wrong. constexpr implies function inline. inline functions must defined in every translation unit it's used. if include header in translation unit other source.cpp , use function, translation unit lacks definition.

so, solution move implementation header. no need worry multiple definition, since function inline.

it doesn't technically need in same file, because definition must in every file uses function, it's simplest define in same header.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -