[英]How to std::bind() to create a data member?
我用C ++ 11很好的新生成器和发行版生成随机值。 在一个函数中,它就像一个魅力,看起来像这样:
void foo() {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
auto dice = bind(distribution, generator);
// dice() will now give a random unsigned value
}
但是,如何将所有三个对象作为数据成员放在一个类中呢? 我可以简单地将generator
和distribution
编写为数据成员,但是如何在不知道(或想知道)其确切类型的情况下使dice
成为数据成员? 这令人惊讶
class X {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
decltype(bind(distribution, generator)) dice;
};
产生错误error C2660: 'bind' : function does not take 2 arguments
在Visual Studio 2013 error C2660: 'bind' : function does not take 2 arguments
。
你总是可以喘气写一个函数,而不是使用一个lambda /绑定的/ etc:
class X {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
public:
auto dice() -> decltype(distribution(generator)) {
return distribution(generator);
}
// or alternatively
auto operator() () -> decltype(distribution(generator)) {
return distribution(generator);
}
};
用于参数化生成器和/或分布类型以及使用std::shared_ptr
保持生成器的加值点,以便您可以创建具有共享相同引擎的不同分布的多个对象。 你最终还是想要一个构造函数来为种子生成种子 - 理想情况下使用像std::random_device{}()
。
或者, 我认为您正在寻找的答案 :
class X {
mt19937 generator{std::random_device{}()};
uniform_int_distribution<unsigned> distribution{1,6};
public:
decltype(bind(std::ref(distribution), std::ref(generator))) dice{
bind(std::ref(distribution), std::ref(generator))
};
};
对不起,我嘲笑你试图首先使用bind
:实际上你可以用C ++ 11中的“无代码”来编写这个类。 我们需要在C ++ 17中获取类成员声明的类型推断,这可能是:
class X {
auto generator = mt19937{std::random_device{}()};
auto distribution = uniform_int_distribution<unsigned>{1,6};
public:
auto dice = bind(std::ref(distribution), std::ref(generator));
};
鉴于最新的Concepts Lite论文建议在auto
可能看起来意味着“推断类型,如果类型没有为命名概念建模而形成错误的语言”的任何地方使用概念名称,那么auto
成员声明可能不是不可能的。
std::bind
的结果未指定:这意味着您无法在没有类型推断的情况下存储其原始结果。 但是,您可以使用std::function
来封装bind
的结果:
#include <functional>
std::function<unsigned()> dice(std::bind(distribution, generator));
auto result = dice();
编辑:正如上面所说的那样,这显然是Visual Studio的问题。 我可以确认这与VS2013一起编译:
#include <functional>
#include <random>
using namespace std;
class X {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
std::function<unsigned()> dice;
public:
X() : dice(bind(distribution, generator)) {}
unsigned roll() { return dice(); }
};
但是将dice
的类型改为decltype(bind(distribution, generator))
会使整个事情失败并伴随着绚丽的色彩(即使它仍然适用于clang)。
它适用于GCC 。 我很确定这只是一个编译器错误。 不幸的是,这意味着您必须咬下苦药并使用其他答案中描述的解决方法之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.