Hy@all,我们要实现如下函数调用:
std::mt19937 engine;
color_table lut = create_random_color_map(engine);
发动机必须是可更换的。我们尝试以这种方式实现它:
*.hpp
#include <tuple>
#include <random>
typedef std::tuple<unsigned char,unsigned char,unsigned char> rgb_tuple;
class color_table{
public:
[...]
void generate(rgb_tuple predicate(const unsigned char& index));
};
template <class RANDOM>
static rgb_tuple random_color(RANDOM engine){
std::uniform_int_distribution<int> dist1 (0,255);
unsigned char red = (unsigned char) dist1();
unsigned char green = (unsigned char) dist1(engine);
unsigned char blue = (unsigned char) dist1(engine);
return std::make_tuple(red, green, blue);
}
template <class RANDOM>
static color_table create_random_color_map(RANDOM engine){
color_table lut;
lut.generate([&](const unsigned char& i)->rgb_tuple {
return random_color<decltype(engine)>(engine);
} );
return lut;
}
* .cpp
...
void color_table::generate(rgb_tuple predicate(const unsigned char& index)){
for(int i = 0; i < 256; ++i){
std::tie(red_table[i], green_table[i], blue_table[i]) = predicate(i);
}
}
当我们尝试编译时,会出现以下错误:
错误 C2664: 'color_table::generate': 参数 1 从 'create_random_color_map::' 到 'rgb_tuple (__cdecl *)(const unsigned char &)' 的转换不可能 1>
没有可用的用户定义转换运算符,可以执行转换或无法调用运算符。... 函数模板“color_table create_random_color_map(RANDOM)”。与 [
随机=std::mt19937]
我们对这次失败完全一无所知,在这种情况下,谷歌不是我们的朋友!:/
我们感谢任何帮助!
问候海米尔