c++ - Linker error with static constant that doesn't seem to be odr-used -


the definition in standard odr-used pretty confusing when details (at least, me is). rely on informal definition of "if reference taken", except when lvalue-to-rvalue conversion available. integral constants, should treated rvalues, seems should excluded reference rule. here sample code failing link:

class test { public:     test();     static constexpr int min_value { 5 };     int m_othervalue = 10; };  test::test() {     m_othervalue = std::max(m_othervalue, min_value); }  int main() {     test t; } 

and linker error get:

 clang++ -std=c++14 -o2 -wall -pedantic -pthread main.cpp && ./a.out /tmp/main-e2122e.o: in function `test::test()': main.cpp:(.text+0x2): undefined reference `test::min_value' clang: error: linker command failed exit code 1 (use -v see invocation) 

live sample: http://coliru.stacked-crooked.com/a/4d4c27d6b7683fe8

why definition of min_value required? it's constant literal value, compiler should optimize out std::max(m_othervalue, 5). don't it.

std::max takes arguments reference, not value. performing lvalue-to-rvalue conversion , constructing temporary object rvalue not allowed. std::max could checking 2 arguments references same object, compiler knows, , check required evaluate true if called std::max(min_value, min_value).


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -