#356 std::min failed to use static member when built in debug

v1.0 (example)
closed-invalid
nobody
None
5
2013-11-01
2013-10-30
Kimi Arthur
No

I use MinGW-builds' binaries.

See the following code. It will compile and run with release settings but will not compile with debug settings. The error is: error: undefined reference to `A::t'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:::C++

include <iostream>

class A
{
static const int t = 12;
public:
static int good()
{
return std::max(t, 13);
}
};

int main(int argc, char *argv[])
{
std::cout << A::good() << std::endl;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Discussion

  • niXman

    niXman - 2013-11-01

    As far as I understand:

    9.4.2 Static data members
    3 If a non-volatile const static data member is of integral or enumeration type, ...
    The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.
    4 [ Note: There shall be exactly one definition of a static data member that is odr-used (3.2) in a program; no diagnostic is required. —end note ]

    3.2 One definition rule
    A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied.

    There is no lvalue-to-rvalue conversion, then 'A::t' is odr-used, hence he needs a separate definition, and it has external linkage.

    Closed.

     
  • niXman

    niXman - 2013-11-01
    • status: open --> closed-invalid
     

Log in to post a comment.