c ++ 11在函数参数中传递了std :: bind

时间:2014-04-26 18:37:57

标签: c++ c++11

可以传入参数std :: bind?

我试试:

主要代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <random>

#include <typeinfo> 
#include <functional>
#include <type_traits>

// config random generator
std::default_random_engine generator;
const char *sequence = "rgbn";
const int sizeChar = strlen( sequence)-1;
std::uniform_int_distribution<int> distribution (0, sizeChar);
auto dice = std::bind ( distribution, generator);
std::cout << "type of dice : " << typeid(dice).name() << std::endl;

// i try that
std::bind< std::uniform_int_distribution<int>(  std::default_random_engine)  > dice2 = std::bind ( distribution, generator);
// or that 
std::bind< std::uniform_int_distribution<int>,  std::default_random_engine  > dice3 = std::bind ( distribution, generator);

Map bibi;
//in a loop
randomInitializationGenerator(bibi, sequence, dice); 

汽车的类型是:

  

St5_BindIFSt24uniform_int_distributionIiESt26linear_congruential_engineIjLj16807ELj0ELj2147483647EEEE

功能代码:

void  randomInitializationGenerator( Map& aremplir, const char* sequence, 
//std::bind< std::uniform_int_distribution<int>( std::default_random_engine) > dice)
std::function< std::uniform_int_distribution<int>( std::default_random_engine) > dice )
//std::function< std::uniform_int_distribution<int>, std::default_random_engine > dice )
{
  const int* sizer          = aremplir.size();
  const unsigned int finRow = sizer[1];
  const unsigned int finCol = sizer[0];
  for(unsigned int y = 0; y < finCol; ++y)
    for(unsigned int x = 0; x < finRow; ++x)
      aremplir.set(y, x, sequence[ dice()]);
}

randomInitializationGenerator param中没有好的类型,因此如果我尝试使用dice(),则无法auto dice = std::bind ( distribution, generator)

其他方式 std::bind< std::uniform_int_distribution<int>( std::default_random_engine) > dice2 = std::bind ( distribution, generator);生成编译错误no the right type

error required from 'typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = std::uniform_int_distribution<int>(std::linear_congruential_engine<unsigned int, 16807u, 0u, 2147483647u>); _BoundArgs = {}; typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type = std::_Bind<std::uniform_int_distribution<int> (*())(std::linear_congruential_engine<unsigned int, 16807u, 0u, 2147483647u>)>]'

最好的问候。

1 个答案:

答案 0 :(得分:0)

没有&#34;一个绑定&#34 ;;它是一个功能,而不是一个对象。

如果您确实需要存储结果,请存储std::function

std::function<int()> dice3 = std::bind(distribution, generator);

请注意,dice3没有参数,因为generator已经绑定到distribution,并且没有任何事情要做(毕竟,这是完整的一点! )。