Linker error with a static constant that does not seem to be used by odr

The definition in the standard for odr is used rather confusingly when you go into details (at least for me that is). I usually rely on the informal definition of "If reference is made," unless the lvalue-to-rvalue conversion is available. For integral constants, they should be considered as r values ​​that seem to be excluded from the reference rule. Here is my sample code that is not related:

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 the linker error that I 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 to `Test :: MIN_VALUE '
clang: error: linker command failed with exit code 1 (use -v to see invocation)

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

MIN_VALUE? , std::max(m_otherValue, 5). .

+4
2

std::max , . lvalue-to-rvalue rvalue . std::max , , , true, std::max(MIN_VALUE, MIN_VALUE).

+7

std::max, , , odr

, odr, ... ...

MIN_VALUE , , odr .

+1

Source: https://habr.com/ru/post/1598455/


All Articles