smallB
smallB

Reputation: 17110

Missing operator<< but it's there

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;
}

Upvotes: 1

Views: 609

Answers (1)

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.

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                     = std::numeric_limits<Int_T>::min(), // constant!
                            typename Best_Fit<Int_T>::type Max_Range
                                     = std::numeric_limits<Int_T>::max()  // constant!
        >
class Int
//...
template<class Int_T>
std::ostream& operator<<(std::ostream& out, 
                         const Int<Int_T, 
                                   Best_Fit<Int_T>::type, // type!
                                   Best_Fit<Int_T>::type  // type!
                         >& obj)

I usually recommend that operator overloads of class templates are defined inside the class definition (use friend to 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)

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                     = std::numeric_limits<Int_T>::min(), // constant!
                            typename Best_Fit<Int_T>::type Max_Range
                                     = std::numeric_limits<Int_T>::max()  // constant!
        >
class Int {
   friend                  // allows you to define a free function inside the class
   std::ostream& operator<<( std::ostream& out, 
                             Int const & obj ) {  // Can use plain Int to refer to this 
                                                  // intantiation. No need to redeclare
                                                  // all template arguments
       return out << obj.get_data();
   }
};

Upvotes: 2

Related Questions