我的代码不能被gcc 8编译,但我不明白为什么。
#include <iostream>
#include <algorithm>
#include <random>
using namespace std;
template<class... T>
void diagnose(T... x);
int main()
{
auto d = normal_distribution<double>(0.0, 1.0);
auto g = default_random_engine();
cout << d(g) << endl;
auto gen = [=](){
//diagnose(d, g);
return d(g); // ******
};
cout << gen() << endl;
}
错误信息显示(指向
*******
):
error: no match for call to â(const std::normal_distribution<double>) (const std::linear_congruential_engine<long unsigned int, 16807, 0, 2147483647>&)
但是,如果我将捕获更改为引用捕获,则代码可以工作。
如果我取消注释
//diagnose
行,错误消息如下(还需要更改
return d(g)
到
return 1.0
):
undefined reference to `void diagnose<std::normal_distribution<double>, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >(std::normal_distribution<double>, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>)'
如您所见,在按值捕获的情况下,参数
g
是常量引用。但是
const
不出现在诊断中。
有人能解释一下这是怎么回事吗?