Default-construct inline static random number engines in gcc
Asked Answered
E

1

12

For example,

#include <random>

struct stru {
    //inline static std::mt19937 rnd; Oops!
    inline static std::mt19937 rnd{};  
};

int main() {

}

I see no semantic difference between the two, and clang has no problem compiling both. Yet gcc 8.1 produces the following error for the first:

prog.cc:4:30: error: no matching function for call to 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::mersenne_twister_engine()'
       inline static std::mt19937 rnd;
                                  ^~~
    In file included from /opt/wandbox/gcc-8.1.0/include/c++/8.1.0/random:49,
                     from prog.cc:1:
    /opt/wandbox/gcc-8.1.0/include/c++/8.1.0/bits/random.h:437:11: note: candidate: 'constexpr std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::mersenne_twister_engine(const std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&)'
         class mersenne_twister_engine
               ^~~~~~~~~~~~~~~~~~~~~~~
    /opt/wandbox/gcc-8.1.0/include/c++/8.1.0/bits/random.h:437:11: note:   candidate expects 1 argument, 0 provided
    /opt/wandbox/gcc-8.1.0/include/c++/8.1.0/bits/random.h:437:11: note: candidate: 'constexpr std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::mersenne_twister_engine(std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&&)'
    /opt/wandbox/gcc-8.1.0/include/c++/8.1.0/bits/random.h:437:11: note:   candidate expects 1 argument, 0 provided

This is a gcc bug (not my code nor libstdc++), right?

Elna answered 13/5, 2018 at 9:33 Comment(0)
P
9

This is a gcc bug (not my code nor libstdc++), right?

Right. It can be much more easily reproduced with this short snippet:

struct test {
    explicit test() {};
};

struct stru {
    inline static test t; 
};

int main() {
    test t;
}

The explicit specifier is throwing GCC off. The same c'tor must be called both for initializing the static inline member and the local variable. And yet GCC initializes the local variable just fine, but complains about the inline static member.

Pun answered 13/5, 2018 at 9:52 Comment(2)
I'm pouring over GCC's Bugzilla, but no luck yet. It might be an ureported one.Pun
Thanks for confirming. For now, I will just use the {} workaround.Elna

© 2022 - 2024 — McMap. All rights reserved.