c++11 - Can't Pass Enum Value to Recursive Template (C++) -


suppose want write recursive template function compares individual value every single element in n-dimensional vector, returning true if there @ least 1 match , false if there none.

i wrote code this, although it's far optimal:

template <typename t, typename checker> void check_for_each(const t& v, const checker condition) {   condition(v); }  template <typename t, typename checker> void check_for_each(const std::vector<t>& v, const checker condition) {   for(unsigned int = 0; < v.size(); i++)   {     check_for_each(v[i], condition);   } }  template <typename t, typename u> bool is_equal_any(const t& value, const std::vector<typename u> vector) {   bool is_equal = false;   check_for_each(vector, [&is_equal, &value](const t& val)   {     if(!is_equal && value == val)     {       is_equal = true;     }   });   return is_equal; } 

while seems work, i've encountered unusual issue , can't quite understand it. example, following code works:

enum piece_name {empty, pawn, rook, knight, bishop, queen, king}; std::vector<std::vector<int>> board {{rook, bishop}, {knight, queen}}; std::cout << is_equal_any(2, board); // outputs 1 (there rook on board) 

yet following, slight change not:

std::cout << is_equal_any(rook, board); // compile error c2664 

apparently function cannot figure out how convert enum value integer. of course, can use static_cast<int>(rook), , code compiles , runs expected, that's not ideal. furthermore, know can change board's declaration std::vector<std::vector<piece_name>> board, (which runs expected) i'd prefer leave @ int. so possible rewrite these recursive template functions is_equal_any can take enum values directly? i'm still new c++, appreciate detail in answer possible. thank you.

the problem arises type t here:

check_for_each(vector, [&is_equal, &value](const t& val)                                                  ^ 

by calling

is_equal_any(rook, board) 

t piece_name, passing parameter lambda elements of vector of type int. int can't implicitly converted enum.

you can't either use directly u std::vector<int> or std::vector< std::vector<int> > or...

if using c++14, use generic lambda auto:

check_for_each(vector, [&is_equal, &value](const auto& val) 

but tagged question c++11, can use trait:

template <typename t> struct leaf_type {     using type = t; };  template <typename t> struct leaf_type<std::vector<t>> {     using type = typename leaf_type<t>::type; };  template <typename t> using leaf_type_t = typename leaf_type<t>::type; 

usage:

check_for_each(vector, [&is_equal, &value](const leaf_type_t<u> & val) 

demo

btw should avoid nested std::vectors , linearize single 1 like:

std::vector<int> board {rook, bishop, knight, queen}; 

then can use std::find.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -