S
Shriramana Sharma
Hello.
I am trying to define an operator<< in my program such that for any container_template_type and value_type, if con is an object of type container_template_type<value_type> and val1, val2, val3 are objects of type value_type, I can chain adding the objects to the container:
con << val1 << val2 << val3 ;
So I tried defining it as:
template < typename C, typename V >
inline C<V> &
operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con; }
but both GCC 4.8.1 and Clang 3.2 complain about this. GCC says "error: ‘C’ is not a template" and Clang says "error: expected unqualified-id".
I am not sure how to tell the compiler that C is a template type. Please help.
The code:
# include <iostream>
# include <vector>
template < typename C, typename V >
inline C<V> &
operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con; }
int main ()
{
vector<int> v ;
v << 1 << 2 << 3 ;
for ( int i = 0 ; i < v.size() ; ++i ) std::cout << v << ' ' ;
std::cout << std::endl ;
}
I am trying to define an operator<< in my program such that for any container_template_type and value_type, if con is an object of type container_template_type<value_type> and val1, val2, val3 are objects of type value_type, I can chain adding the objects to the container:
con << val1 << val2 << val3 ;
So I tried defining it as:
template < typename C, typename V >
inline C<V> &
operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con; }
but both GCC 4.8.1 and Clang 3.2 complain about this. GCC says "error: ‘C’ is not a template" and Clang says "error: expected unqualified-id".
I am not sure how to tell the compiler that C is a template type. Please help.
The code:
# include <iostream>
# include <vector>
template < typename C, typename V >
inline C<V> &
operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con; }
int main ()
{
vector<int> v ;
v << 1 << 2 << 3 ;
for ( int i = 0 ; i < v.size() ; ++i ) std::cout << v << ' ' ;
std::cout << std::endl ;
}