c++ - Workaround for lack of expression SFINAE -
i'm trying call function each value in std::tuple
, of course there no way iterate tuple , i've resorted using template techniques discussed in iterate on tuple
however, i'm using visual studio 2013 , not support expression sfinae, code won't work. i've tried partially specialize templates based on constant numbers (e.g 5, 4, 3, 2, 1, 0), haven't had success. i'm no template expert, , hoping me out. expression sfinae code below.
#include <iostream> #include <tuple> using namespace std; struct argpush { void push(bool x) {} void push(int x) {} void push(double x) {} void push(const char* x) {} void push(const std::string& x) {} template<std::size_t = 0, typename... tp> inline typename std::enable_if<i == sizeof...(tp), void>::type push_tuple(const std::tuple<tp...>& t) { } template<std::size_t = 0, typename... tp> inline typename std::enable_if<i < sizeof...(tp), void>::type push_tuple(const std::tuple<tp...>& t) { push(std::get<i>(t)); push_tuple<i + 1, tp...>(t); } }; int main() { argpush().push_tuple(std::make_tuple(1,2,3,4)); argpush().push_tuple(std::make_tuple("hello", "msvc makes me sad", 4, true)); return 0; }
msvc doesn't equality comparisons being made within enable_if
. move these out there helper template. code compiles on vs2013.
template<std::size_t i, typename... tp> struct comparator { static const bool value = (i < sizeof...(tp)); }; template<std::size_t = 0, typename... tp> inline typename std::enable_if<!comparator<i, tp...>::value>::type push_tuple(const std::tuple<tp...>& t) { } template<std::size_t = 0, typename... tp> inline typename std::enable_if<comparator<i, tp...>::value>::type push_tuple(const std::tuple<tp...>& t) { push(std::get<i>(t)); push_tuple<i + 1, tp...>(t); }
Comments
Post a Comment