Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference for static constexpr on g++ 4.9 with no optimisation

Tags:

c++

gcc

c++11

c++14

I have the following code:

#include<chrono>
#include<iostream>

using namespace std::chrono_literals;

#define MSG "hello"
#define DUR 1000ms

class mwe{
    public: 
    static constexpr auto msg = MSG;
    static constexpr auto dur_1 = DUR;
    static constexpr std::chrono::milliseconds dur_2 = DUR;
    static const std::chrono::milliseconds dur_3;
    static constexpr decltype(DUR) dur_4 = DUR;
};

constexpr std::chrono::milliseconds mwe::dur_2; 
const std::chrono::milliseconds mwe::dur_3 = DUR; 
constexpr decltype(DUR) mwe::dur_4;

int main(void) {
    std::cout << "str: " << mwe::msg << std::endl;
    std::cout << "dur_1: " << mwe::dur_1.count() << std::endl;
    std::cout << "dur_2: " << mwe::dur_2.count() << std::endl;
    std::cout << "dur_3: " << mwe::dur_3.count() << std::endl;
    std::cout << "dur_4: " << mwe::dur_4.count() << std::endl;
}

If I compile it (g++ 4.9), via

g++ -std=c++14 -O2 test.cpp

everything works like expected, but if I compile it via

g++ -std=c++14 -O0 test.cpp

I get the following Error:

undefined reference to `mwe::dur_1'

I personally like the way, dur_1 is defined and declared most, but it doesn't work with g++ in my version, if no optimisations are enabled. Because all other ways I know (dur_2, dur_3, dur_4) have their drawbacks (redundancy of the value, no auto type deduction, if I would for example change 1000ms to 1s, aso.)

Do you know, if this is a gcc bug, that the compilation works on production mode, but doesn't work without optimisation?

And is there anoter possible way of getting this working, without defining the location for dur_x outside of the class?

like image 543
byteunit Avatar asked Feb 10 '23 20:02

byteunit


1 Answers

It is not a bug in your compiler.

You odr-used dur1 but never defined it. And, neither the constexpr nor the inline initialiser make that declaration a definition.

[C++11, C++14: 9.4.2/3]: If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.19). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. —end note ] 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.

As always, optimisation levels may affect the extent to which the compiler is able and/or willing to report such mistakes.

You may write the following to define your member:

#include<chrono>

using namespace std::chrono_literals;

#define DUR 1000ms

struct T
{
   static constexpr auto dur_1 = DUR;
};

constexpr decltype(T::dur_1) T::dur_1;
like image 175
Lightness Races in Orbit Avatar answered Feb 13 '23 04:02

Lightness Races in Orbit