Я хочу перегрузить operator <<()
для моего собственного класса, который также является шаблоном. Мои занятия следующие:
template<
typename RefCountType,
typename TraitsType = std::char_traits<char>,
typename Allocator = std::allocator<typename TraitsType::char_type>
>
class rep {
// ...
};
template<typename RepType>
class t_zstring {
// ...
};
Код для operator<<()
:
template<
typename CharType,
typename TraitsType,
typename Allocator,
typename RefCountType,
template<class,class,class> class RepType
>
inline std::basic_ostream<CharType,TraitsType>&
operator<<( std::basic_ostream<CharType,TraitsType> &os,
t_zstring< RepType<RefCountType,TraitsType,Allocator> > const &s ) {
return os << s.c_str();
}
Код шаблона компилируется просто отлично; однако, когда я пытаюсь использовать это как (с кодом для класса my_ref_count
, исключенным):
typedef rep<my_ref_count> my_rep;
typedef t_zstring<my_rep> zstring;
zstring z;
cout << z; // ztest.cpp, line 201
Я получаю (используя g ++ 4.2.1):
ztest.cpp:201: error: no match for ‘operator<<’ in ‘std::cout << z1’
Как правильно объявить мой operator<<()
, чтобы компилятор нашел правильное совпадение?