By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::stringstrArray;
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::stringintArray;
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::stringstrArr;
//GetArray( strArr );

std::vector<intintArr;
GetArray<int>( intArr );

return 0;
}

/////

error: no matching function for call to 'std::vector<int,
std::allocator<int::push_back(std::basic_string<ch ar,
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>]

////

Mar 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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::stringargument 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<intor
vector<stringand call a function that converts an array of string to
an array of integers...

i was just hoping for something a bit smarter... ;-)

Mar 2 '07 #2

P: n/a
* ma*****@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::stringstrArray;
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::stringintArray;
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?
Mar 2 '07 #3

P: n/a
you can use partial specialization.

ma*****@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::stringargument 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<intor
vector<stringand call a function that converts an array of string to
an array of integers...

i was just hoping for something a bit smarter... ;-)
Mar 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.