Undefined symbols for constexpr function

前端 未结 3 961
[愿得一人]
[愿得一人] 2021-01-04 02:16

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

3条回答
  •  感动是毒
    2021-01-04 03:06

    Why does declaring the function constexpr cause a linker error?

    That is because constexpr functions are implicitly inline. Per Paragraph 7.1.5/2 of the C++11 Standard:

    A constexpr specifier used in the declaration of a function that is not a constructor declares that function to be a constexpr function. Similarly, a constexpr specifier used in a constructor declaration declares that constructor to be a constexpr constructor. constexpr functions and constexpr constructors are implicitly inline (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). [...]

提交回复
热议问题