templated std::vector variable as an argument by reference problem

M

mast2as

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

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.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>]

////
 
M

mast2as

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... ;-)
 
A

Alf P. Steinbach

* (e-mail address removed):
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 );
}
}


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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top