Undefined symbols for constexpr function

前端 未结 3 963
[愿得一人]
[愿得一人] 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:13

    It's an interesting question. As Andy Prowl, constexpr makes the function inline, which means that there must be a definition of it in every translation unit which uses it; I would have expected an error from the compiler. (Actually, if I read §3.2/5 correctly, a diagnostic is required if you use the function and there is no definition.)

    As to why const has different behavior: you can't mark a non-menber function const. If you write const int Foo();, it is not the function which is const, but the type it returns (except that if the return type is not a class type, cv-qualifiers are ignored, so this is really the same as int Foo();).

提交回复
热议问题