2017-05-16 16 views
0

複数の異なるタイプのC++ディストリビューションを1つのコンテナに格納しようとしていますが、この目的でstd :: functionを使用できると思いました。次のようにこれを行うことで、私の試みは、次のとおりです。std :: vectorへのstd :: normal_distributionをstd :: vectorに格納するためにバインドする

void print_rand(){ 
    // Make the random number generator 
    std::random_device rd{}; 
    std::mt19937 engine(rd()); 

    // Store the distribution in a std::function 
    auto n1 = std::make_shared<std::normal_distribution<double>>(0,1); 
    std::function<double (std::mt19937)> fn = std::bind(&std::normal_distribution<double>::operator(), n1, std::placeholders::_1); 

    // Call the function to get a normally distributed number 
    std::cerr << fn(engine) << std::endl; 
} 

を私は次のエラーを取得する:

error: no matching function for call to ‘bind(<unresolved overloaded function type>, std::shared_ptr<std::normal_distribution<double> >&, const std::_Placeholder<1>&)’ 
    std::function<double (std::mt19937)> fn = std::bind(&std::normal_distribution<double>::operator(), n1, std::placeholders::_1); 

私はオペレータを専門にテンプレートを(しようとしたとき)のように呼び出す:

std::function<double (std::mt19937)> fn = std::bind(&std::normal_distribution<double>::operator()<std::mt19937>, n1, std::placeholders::_1); 

I同じエラーが発生します。

ヒントは本当にありがとう!

答えて

3

は、ここでは、std::functionnormal_distributionを置く方法は次のとおりです。

std::normal_distribution<double> nd(0, 1); 
std::function<double(std::mt19937&)> fn = nd; 

ノーshared_ptr、ノーbind、ノーラムダ、乱数エンジンの無いコピー(&に注意してください)。

+0

これはずっと簡単です!受け入れられた答えをこれに変更する。 1つの質問:std :: functionはディストリビューションのコピーを作成し保持しますか? – acorso

+0

@acorsoはい.... –

関連する問題