c++ - Template Conversion Failure with Lambda Function -


hy @ all, have realize following functioncall:

std::mt19937 engine; color_table lut = create_random_color_map(engine); 



engine has exchangeable. tried implement way:
*.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 = 0; < 256; ++i){         std::tie(red_table[i], green_table[i], blue_table[i]) = predicate(i);     } } 

when try compile, following error occurs:

error c2664: 'color_table::generate': convertion of parameter 1 'create_random_color_map::' 'rgb_tuple (__cdecl *)(const unsigned char &)' not possible 1>
no userdefined convertionoperator available, can execute convertion or operator cannot invoked. ... function-template "color_table create_random_color_map(random)". [
random=std::mt19937 ]

we clueless failure , google isn't our friend in case! :/

we appreciate help!

regards hymir

first (major) issue:

your function generate() accepts function pointer argument, , trying pass capturing lambda. unfortunately, capturing lambdas cannot converted implicitly function pointers. per paragraph 5.1.2/6 of c++11 standard:

the closure type lambda-expression with no lambda-capture has public non-virtual non-explicit const conversion function pointer function having same parameter , return types closure type’s function call operator. value returned conversion function shall address of function that, when invoked, has same effect invoking closure type’s function call operator.

you change design , let generate() become function template, accepting callable object argument:

 class color_table{     public:         template<typename p>         void generate(p predicate);  }; 

alternatively, use std::function wrapper achieve flexibility without recurring templates (but run-time overhead):

#include <functional> // <== needed std::function  class color_table{     public:         void generate(std::function<rgb_tuple(const unsigned char&)> predicate); }; 

here live example shows code compiling above solution.

second (minor) issue:

you should not specify template arguments random_color() explicitly. rather let compiler job , perform type deduction:

lut.generate([&](const unsigned char& i)->rgb_tuple {     return random_color(engine); //         ^^^^^^^^^^^^^^^^^^^^^ }); 

Comments

Popular posts from this blog

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -

python - How to create a legend for 3D bar in matplotlib? -