Sunday, June 23, 2013

Trying to define a generic template operator for all container classes

Newsgroup: comp.lang.c++

Subject: Trying to define a generic template operator for all container classes

From: Shriramana Sharma <samjnaa@...>

Date: Sun, 23 Jun 2013 00:21:29 -0700 (PDT)



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[i] << ' ' ;

std::cout << std::endl ;

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?24761-Trying-to-define-a-generic-template-operator-for-all-container-classes&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment