Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22067

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 22067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>