Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default-construct inline static random number engines in gcc

For example,

#include <random>

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

int main() {

}

I see no semantic difference in the two, and clang has no problem in 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?

like image 939
Lingxi Avatar asked May 13 '18 09:05

Lingxi


1 Answers

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.

like image 184
StoryTeller - Unslander Monica Avatar answered Nov 15 '22 11:11

StoryTeller - Unslander Monica