When I attempt compiling the following code I get a linker error: Undefined symbols for architecture x86_64: \"Foo()\", referenced from: _main in main.o using L
Why does declaring the function
constexprcause a linker error?
That is because constexpr functions are implicitly inline. Per Paragraph 7.1.5/2 of the C++11 Standard:
A
constexprspecifier used in the declaration of a function that is not a constructor declares that function to be aconstexprfunction. Similarly, aconstexprspecifier used in a constructor declaration declares that constructor to be aconstexprconstructor.constexprfunctions andconstexprconstructors are implicitlyinline(7.1.2).
Per Paragraph 7.1.2/4, then:
An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [...]