Reputation: 185
I have a static constexpr member function that i declare in the .h file. If i define the function right away in the header file, everything works perfectly. I have the general inclination to define functions in the .cpp file (even if i want them inlined, i would use the inline keyword and again do so) so i when i try to do it, it seems okay at first but when i try to call this function i get the following error:
static constexpr uint16_t ClassA::myFoo()' used before its definition
I would like learn if there is a way to define a static constexpr member function in the .cpp file rather than the header. If thats not possible or maybe limited due the compiler i am using, is there any side effects defining the function in the .h file ? (I know it is explicitly inline for normal functions but i am not sure for a constexper static function).
PS: I am using arm-none-eabi-g++ (c++17) and Clion for a small embedded project.
Upvotes: 1
Views: 2053
Reputation: 238431
I would like learn if there is a way to define a static constexpr member function in the .cpp file rather than the header.
Yes... but you must define the function in every TU where it is used because it is an inline function. As such, it is simpler to put the definition into a header so that same definition will be included into all the TUs that need it.
It is an inline function because constexpr functions are implicitly inline functions - i.e. whether or not you use the inline
keyword.
is there any side effects defining the function in the .h file ?
The effect of doing this is that the function definition will be included into every TU that include the header. I don't quite understand what you mean by "side" effect in this context.
Upvotes: 5