std::seed_seq编译错误(GCC错误?)
std::seed_seq compilation error (GCC bug?)
我有以下几行代码,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(Red Hat 6.3.1-1)
- gcc(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609
VS2013更新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
很明显,您传递给engine.seed()
的参数就是问题所在
我建议你看看
种子()
让我们简单了解一下原因
正如您所看到的,有两种方法可以调用由以下定义的seed()
:
void seed( result_type value = default_seed );
和
void seed( Sseq& seq );
如果你担心gcc中有一个bug,那么我认为你不应该
事实上Clang 3.9.1(只是提到一个版本)也给出了类似的错误:
Start
prog.cc:32:12: error: no matching member function for call to 'seed'
engine.seed(std::seed_seq{ rdSeed, threadID, now });
~~~~~~~^~~~
/opt/wandbox/clang-3.9.1/include/c++/v1/random:2116:10: note: candidate function not viable: no known conversion from 'std::seed_seq' to 'result_type' (aka 'unsigned long') for 1st argument
void seed(result_type __sd = default_seed);
^
/opt/wandbox/clang-3.9.1/include/c++/v1/random:2124:9: note: candidate function [with _Sseq = std::__1::seed_seq] not viable: expects an l-value for 1st argument
seed(_Sseq& __q)
^
1 error generated.
1
Finish
并准确地指出问题所在
我发现gcc和clang不太可能有相同的错误,但一切都有可能
要了解如何将seed()
与std::seed_seq
组合调用,您应该查看下面的seed(),它有一个类似的例子
问题可以解决,正如您已经提到的
std::seed_seq seq{ rdSeed, threadID, now };
engine.seed(seq);
或者(这是一种冗长的方式,我可能不会这么做):
std::seed_seq seq{ rdSeed, threadID, now };
std::seed_seq& pr = seq;
engine.seed(pr);
要了解更多关于这种情况/错误的信息,我建议您阅读以下SO帖子:
类型';的非常量引用的初始化无效;int';从类型为';int';
正如@Ashe the human在评论中指出的那样,VS2013允许它编译,因为它是默认启用的语言扩展:它应该发出警告,让人们知道。您可以在这里阅读更多关于它的信息:
绑定到临时Visual Studio bug的非常量引用?
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 初始化迭代器错误 C++ 在 GCC 编译器中
- GCC:随机构建导致执行期间分段错误
- 错误:使用 clang 没有可行的重载,使用 GCC 编译
- "未定义对'WinMain@16'的引用"gcc 编辑器中的错误
- 与NVIDIA链接的错误GCC版本
- std::seed_seq编译错误(GCC错误?)
- 使用 lambda 的错误 gcc 行为
- std :: sort函数上的编译器错误(GCC 4.4)
- C++ 编译错误(gcc 4.7)
- 套接字错误 gcc 无法在 WinSock2.h 中获取函数
- Linux编译错误-GCC 4.3.4-模板参数列表太少
- 如何解决在Zero C Ice编译中的upCast错误?- Gcc - 4.7使用clang编译
- GCC 6.1.0 分段错误 - GCC 错误
- std::bind 编译器错误 gcc
- VS2013过载错误(GCC不会发生)
- 使用Python.h编译错误gcc