OutputIterator value_type

F

FabioAng

Assuming I have this function:

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = *input;
++result;
++input;
}
}

I would like to cast input value to output value in this way

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = (cast to output value type) *input;
++result;
++input;
}
}

Any suggestions ?

Regards,
Fabio
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Assuming I have this function:

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = *input;
++result;
++input;
}
}

I would like to cast input value to output value in this way

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = (cast to output value type) *input;
++result;
++input;
}
}

Normally the output iterator has a value_type associated with it,
which is the type you want. So what you want might be something like
this (untested):

*result = static_cast<typename OutputIterator::value_type>(*input);
 
D

David Harmon

On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"
I would like to cast input value to output value in this way

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = (cast to output value type) *input;

*result =
static_cast<std::iterator_traits<OutputIterator>::value_type>(*input);
 
P

Pete Becker

FabioAng said:
Assuming I have this function:

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = *input;
++result;
++input;
}
}

I would like to cast input value to output value in this way

template<typename OutputIterator>
void copy(const char* input, OutputIterator result)
{
while ( *input != NULL )
{
*result = (cast to output value type) *input;
++result;
++input;
}
}

Output iterators don't generally have a well-defined value type that you
can get at. Technically, the requirement for an output iterator that
applies here is that the expression *iter = val; must be valid and must
copy val to the location in the output sequence that iter points to. If
you can't assign the value, then the iterator just isn't the right type.

Take a step back, and describe the problem that you're trying to solve,
rather than an attempted solution that didn't work.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Pete Becker

David said:
On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"


*result =
static_cast<std::iterator_traits<OutputIterator>::value_type>(*input);

This doesn't work: an output iterator's value_type isn't necessarily
meaningful. For example, ostream_iterator's value_type is void.

The reason for this is that there are typically many different types
that can be assigned through an output iterator, and picking one of them
wouldn't be particularly helpful. The relevant requirement for an output
iterator is that *result = val must be valid.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top