【发布时间】:2017-06-27 03:34:47
【问题描述】:
我有以下代码行,最新版本的 GCC 无法编译。我很确定它甚至没有发出警告消息。
seed_seq.cpp:
void func()
{
std::mt19937_64 engine;
std::hash<std::thread::id> hasher;
uint64_t rdSeed, threadID, now;
try
{
std::random_device rd;
if (rd.entropy())
rdSeed = rd();
else
rdSeed = 0;
}
catch (std::exception &)
{
rdSeed = 0;
}
threadID = hasher(std::this_thread::get_id());
now = std::chrono::system_clock::now().time_since_epoch().count();
engine.seed(std::seed_seq{ rdSeed, threadID, now });
}
命令:gcc -std=c++11 -c seed_seq.cpp -o seed_seq.o
无法编译的GCC版本:
- gcc (GCC) 6.3.1 20161221(红帽 6.3.1-1)
- gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
VS2013 Update 5 没有抱怨。确定是 GCC 的错?
我不记得有效的 GCC 版本。对不起。
错误:
seed_seq.cpp: In function ‘void func()’:
seed_seq.cpp:28:19: error: invalid initialization of non-const reference of type ‘std::seed_seq&’ from an rvalue of type ‘std::seed_seq’
engine.seed(std::seed_seq{ rdSeed, threadID, now });
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6.3.1/random:51:0,
from seed_seq.cpp:2:
/usr/include/c++/6.3.1/bits/random.tcc:353:7: note: initializing argument 1 of ‘typename std::enable_if<std::is_class<_Sseq>::value>::type std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::seed(_Sseq&) [with _Sseq = std::seed_seq; _UIntType = long unsigned int; long unsigned int __w = 64ul; long unsigned int __n = 312ul; long unsigned int __m = 156ul; long unsigned int __r = 31ul; _UIntType __a = 13043109905998158313ul; long unsigned int __u = 29ul; _UIntType __d = 6148914691236517205ul; long unsigned int __s = 17ul; _UIntType __b = 8202884508482404352ul; long unsigned int __t = 37ul; _UIntType __c = 18444473444759240704ul; long unsigned int __l = 43ul; _UIntType __f = 6364136223846793005ul; typename std::enable_if<std::is_class<_Sseq>::value>::type = void]’
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__s, __b, __t, __c, __l, __f>::
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
更新
在系统的 dnf.log 中找到了这个。不确定哪个是我在项目中使用的...
Nov 01 18:17:41 DEBUG ---> Package gcc-gdb-plugin.x86_64 6.2.1-2.fc24 will be installed
Nov 01 18:17:42 DEBUG ---> Package gcc.x86_64 5.3.1-6.fc23 will be upgraded
Nov 01 18:17:42 DEBUG ---> Package gcc.x86_64 6.2.1-2.fc24 will be an upgrade
Nov 01 18:17:42 DEBUG ---> Package gcc-c++.x86_64 5.3.1-6.fc23 will be upgraded
Nov 01 18:17:42 DEBUG ---> Package gcc-c++.x86_64 6.2.1-2.fc24 will be an upgrade
Nov 01 18:17:42 DEBUG ---> Package libgcc.x86_64 5.3.1-6.fc23 will be upgraded
Nov 01 18:17:42 DEBUG ---> Package libgcc.x86_64 6.2.1-2.fc24 will be an upgrade
Nov 02 11:03:25 DEBUG ---> Package gcc-debuginfo.x86_64 6.2.1-2.fc24 will be installed
Nov 02 11:03:25 DEBUG ---> Package gcc-base-debuginfo.x86_64 6.2.1-2.fc24 will be installed
Jan 04 09:59:34 DEBUG ---> Package gcc.x86_64 6.2.1-2.fc24 will be upgraded
Jan 04 09:59:34 DEBUG ---> Package gcc.x86_64 6.3.1-1.fc24 will be an upgrade
Jan 04 09:59:34 DEBUG ---> Package gcc-gdb-plugin.x86_64 6.2.1-2.fc24 will be upgraded
Jan 04 09:59:34 DEBUG ---> Package gcc-gdb-plugin.x86_64 6.3.1-1.fc24 will be an upgrade
Jan 04 09:59:34 DEBUG ---> Package gcc-c++.x86_64 6.2.1-2.fc24 will be upgraded
Jan 04 09:59:34 DEBUG ---> Package gcc-c++.x86_64 6.3.1-1.fc24 will be an upgrade
Jan 04 09:59:34 DEBUG ---> Package libgcc.x86_64 6.2.1-2.fc24 will be upgraded
Jan 04 09:59:34 DEBUG ---> Package libgcc.x86_64 6.3.1-1.fc24 will be an upgrade
【问题讨论】:
-
哪个 gcc 版本是这里明显的问题。而且,您在编译时是否使用了例如 -Wall -pedantic?哪个错误是“某种”重要的……
engine.seed()做什么?这里例如从序列 seq 开始生成 10 个数字,它使用 generate 方法en.cppreference.com/w/cpp/numeric/random/seed_seq -
您的代码(删除了
this->__ctx->engine.seed(seq);)在 GCC 6.1、6.2、6.3 和 7(快照)上编译正常。 godbolt.org/g/hU4Rz0 -
'seed()' 需要一个引用,“它可能会改变的东西”。你传递了一些它无法改变的东西。
-
@fedepad 它在声明 std::seed_seq 并作为变量传递给 seed() 时起作用。
-
是的,或者你这样做,当然。如此处所示:en.cppreference.com/w/cpp/numeric/random/…