![]() |
templated std::vector variable as an argument by reference problem
sorry i am too sure how to write a more explicit subject but this code
doesn't compile for the type string and I am not sure why (and I am not sure either how to describe the problem but by looking at the program you should understand what I am trying to do easily, which is convert an array of string to an array of another type, either string, float or interger.) I am not sure what I am trying to do is legal. Obvisouly it doesn't seem to be as the compiler complains but I wonder if there's a way i can get it to work ? Thanks for your help. -mark template<typename T> void GetArray( std::vector<T> &array ) { std::vector<std::string> strArray; strArray.push_back( "test1" ); strArray.push_back( "test2" ); if ( typeid( std::string ) == typeid( T ) ) { for ( size_t i = 0; i < strArray.size(); ++i ) { // DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS COMMENTED OUT! //array.push_back( strArray[i] ); } } std::vector<std::string> intArray; intArray.push_back( "1" ); intArray.push_back( "2" ); if ( typeid( int ) == typeid( T ) ) { for ( size_t i = 0; i < intArray.size(); ++i ) { array.push_back( atoi( intArray[i].c_str() ) ); } } } int main() { //std::vector<std::string> strArr; //GetArray( strArr ); std::vector<int> intArr; GetArray<int>( intArr ); return 0; } ///// error: no matching function for call to 'std::vector<int, std::allocator<int> >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)' /usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>] //// |
Re: templated std::vector variable as an argument by reference problem
hum clearly what i am trying to do in the code is completly stupid, as
of course if at runtime we call GetArray with a std::vector<std::string> argument the compiler still tries to make sense of the code where numbers or pushed to this array. So it compiles fines for numbers but then when i write the code for string there's a mismatch (the same array can't be used to push string & numbers). Anyway... if anyone has in mind a mechanism that would allo me to do that be great. Right now the only solution i see if to return a vector of strings and the type of the token which the strings encode (strings or number), then create a vector of that type (vector<int> or vector<string> and call a function that converts an array of string to an array of integers... i was just hoping for something a bit smarter... ;-) |
Re: templated std::vector variable as an argument by reference problem
* mast2as@yahoo.com:
> sorry i am too sure how to write a more explicit subject but this code > doesn't compile for the type string and I am not sure why (and I am > not sure either how to describe the problem but by looking at the > program you should understand what I am trying to do easily, which is > convert an array of string to an array of another type, either string, > float or interger.) > > I am not sure what I am trying to do is legal. Obvisouly it doesn't > seem to be as the compiler complains but I wonder if there's a way i > can get it to work ? > > Thanks for your help. > > -mark > > template<typename T> > void GetArray( std::vector<T> &array ) > { > std::vector<std::string> strArray; > strArray.push_back( "test1" ); > strArray.push_back( "test2" ); > > if ( typeid( std::string ) == typeid( T ) ) > { > > for ( size_t i = 0; i < strArray.size(); ++i ) > { > // DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS > COMMENTED OUT! > //array.push_back( strArray[i] ); > } > } The code above is compiled also for a type T that isn't std::string. Instead of 'if' you can use a specialization of GetArray for type std::string. > std::vector<std::string> intArray; > intArray.push_back( "1" ); > intArray.push_back( "2" ); > > if ( typeid( int ) == typeid( T ) ) > { > for ( size_t i = 0; i < intArray.size(); ++i ) > { > array.push_back( atoi( intArray[i].c_str() ) ); > } > } The code above is compiled also for a type T that isn't integer. Instead of 'if' and 'atoi' you can use a 'boost::lexical_cast' as a generic implementation. > } -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
Re: templated std::vector variable as an argument by reference problem
you can use partial specialization.
mast2as@yahoo.com wrote: > hum clearly what i am trying to do in the code is completly stupid, as > of course if at runtime we call GetArray with a > std::vector<std::string> argument the compiler still tries to make > sense of the code where numbers or pushed to this array. So it > compiles fines for numbers but then when i write the code for string > there's a mismatch (the same array can't be used to push string & > numbers). > > Anyway... if anyone has in mind a mechanism that would allo me to do > that be great. Right now the only solution i see if to return a vector > of strings and the type of the token which the strings encode (strings > or number), then create a vector of that type (vector<int> or > vector<string> and call a function that converts an array of string to > an array of integers... > > i was just hoping for something a bit smarter... ;-) |
| All times are GMT. The time now is 05:00 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.