c++ - Forward declaring a constexpr function in a header -
this question has answer here:
- undefined symbols constexpr function 3 answers
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
Post a Comment