Int this class where operator<< is defined (see code) while trying to compile it with gcc 4.6.1 I’m getting following error: no match for ‘operator<<‘ in ‘std::cout << a’. What’s going on?
template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range = std::numeric_limits<Int_T>::min(),
typename Best_Fit<Int_T>::type Max_Range = std::numeric_limits<Int_T>::max()>
class Int
{
Int_T data_;
Int_T get_data()const
{
return data_;
}
};
//Here is this operator defined
template<class Int_T>
std::ostream& operator<<(std::ostream& out, const Int<Int_T, Best_Fit<Int_T>::type, Best_Fit<Int_T>::type>& obj)
{
out << obj.get_data();
return out;
}
where Best_Fit looks like:
#ifndef BEST_FIT_H_INCLUDED
#define BEST_FIT_H_INCLUDED
struct Signed_Type
{
typedef long long type;
};
struct Unsigned_Type
{
typedef unsigned long long type;
};
template<bool Cond, class First, class Second>
struct if_
{
typedef typename First::type type;
};
template<class First, class Second>
struct if_<false,First,Second>
{
typedef typename Second::type type;
};
template<class Int_T>
struct Best_Fit
{//evaluate it lazily ;)
typedef typename if_<std::is_signed<Int_T>::value,Signed_Type,Unsigned_Type>::type type;
};
#endif // BEST_FIT_H_INCLUDED
edit:
#include <iostream>
int main(int argc, char* argv[])
{
Int<signed char,1,20> a(30);
cout << a;
}
Your template has three arguments, a type, and two constants of a known best fit type, but your templated
operator<<takes an instantiation of the template with three types.I usually recommend that operator overloads of class templates are defined inside the class definition (use
friendto define a free function in that context) for this particular reason, it is trivial to get the types right inside the class template, and easy to fail outside of it. There are a couple other differences (like the fact that if the operator is defined inside the class then it will only be accessible through ADL –unless you also decide to declare it outside)