I am trying to selectively enable a method in a templated class only if the tuple template argument has a certain arity. I kept trying all kind of combinations and so far couldn't find a way to get the code below to compile.
Essentially, when I instantiate the class foo with a tuple that has only two elements in it I would like the first "void print()" method to be enabled, while when I instantiate foo with a tuple that has three elements in it I would like the second "void print()" method to be enabled.
Note: This is just an abstract example refined from a more complex problem I am trying to solve. also I am able to get something to work using partial template specialization based on the two specific tuples. However I would really like to understand "enable_if" which I expect will keep my code "simpler".
Thank you
// g++ -std=c++14 a.cc
#include <tuple>
#include <type_traits>
#include <iostream>
using namespace std;
template <class Tuple>
class foo {
public:
Tuple t;
foo(Tuple ta) : t(ta) {
cout<<"constructor\n";
}
//How do I enable this print ONLY if arity of input tuple is 2 ???
typename std::enable_if< std::tuple_size<Tuple>::value == 2, void >::type
print(){
cout<<get<0>(t)<<":"<<get<1>(t)<<"\n";
}
// enable ONLY if arity 3 ???
typename std::enable_if< std::tuple_size<Tuple>::value == 3, void >::type
print(){
cout<<get<0>(t)<<":"<<get<1>(t)<<"::"<<get<1>(t)<<"\n";
}
};
int main(int argc, char**argv) {
typedef tuple<int, double> t2_type;
typedef tuple<int, double, float> t3_type;
t2_type t2(1,2.0);
t3_type t3(100,100.0,2.3);
foo<t2_type> f2(t2);
foo<t3_type> f3(t3);
f2.print();
f3.print();
}
For the specific code above here is the compiler error:
% g++ -std=c++14 a.cc
a.cc:25:3: error: ‘typename std::enable_if<(std::tuple_size<_Tp>::value == 3), void>::type foo<Tuple>::print()’ cannot be overloaded
print(){
^
a.cc:19:3: error: with ‘typename std::enable_if<(std::tuple_size<_Tp>::value == 2), void>::type foo<Tuple>::print()’
print(){
^
a.cc: In instantiation of ‘class foo<std::tuple<int, double, float> >’:
a.cc:38:21: required from here
a.cc:19:3: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
a.cc: In function ‘int main(int, char**)’:
a.cc:41:6: error: ‘class foo<std::tuple<int, double, float> >’ has no member named ‘print’
f3.print();
^
GCC Details, though this should be compiler independent;
% g++ -v
gcc version 4.9.4 (GCC)
SOLUTION 1 derived from answers below:
#include <tuple>
#include <type_traits>
#include <iostream>
using namespace std;
template <class Tuple>
class foo {
public:
Tuple t;
foo(Tuple ta) : t(ta) {
cout<<"constructor\n";
}
template <typename T = Tuple>
typename std::enable_if< std::tuple_size<T>::value == 2, void >::type
print(){
cout<<get<0>(t)<<":"<<get<1>(t)<<"\n";
}
// enable ONLY if arity 3 ???
template <typename T = Tuple>
typename std::enable_if< std::tuple_size<T>::value == 3, void >::type
print(){
cout<<get<0>(t)<<":"<<get<1>(t)<<"::"<<get<1>(t)<<"\n";
}
};
int main(int argc, char**argv) {
typedef tuple<int, double> t2_type;
typedef tuple<int, double, float> t3_type;
t2_type t2(1,2.0);
t3_type t3(100,100.0,2.3);
foo<t2_type> f2(t2);
foo<t3_type> f3(t3);
f2.print();
f3.print();
}