473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

OutputIterator value_type

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



May 10 '07 #1
4 1766
On 10 Maj, 09:40, "FabioAng" <fabioangNOS...@libero.itwrote:
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);

--
Erik Wikström

May 10 '07 #2
On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"
<fa************@libero.itwrote,
>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);
May 10 '07 #3
FabioAng wrote:
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)
May 10 '07 #4
David Harmon wrote:
On Thu, 10 May 2007 09:40:00 +0200 in comp.lang.c++, "FabioAng"
<fa************@libero.itwrote,
>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);

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)
May 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chyi Pin Lim | last post by:
Why is the type for back_insert_iterator void and not the type of its encapsulating container? Why: namespace std { template<class Category, class T, class Distance = ptrdiff_t, class...
8
by: chris | last post by:
I tried to post this to comp.std.c++ some time ago, but for some reason I aren't getting any automatic confirmation. I thought I would therefore post it here. Some time ago I submitted what is...
5
by: cesco | last post by:
I'm designing an algorithm with maps that need the elements to be sorted by value instead of by key. Does anyone have a suggestion on how to do that? #include <map> using namespace std; int...
4
by: Andrew | last post by:
Hello: Can someone please elaborate on why the member types 'value_type', 'reference', and 'const_reference' are all defined in a standard STL container. Why not just define 'value_type' and...
6
by: Daniel T. | last post by:
The line marked (1) below compiles fine and does exactly what I would expect, but the line marked (2) does not compile at all on my system. error: variable or field 'bar' declared void My...
3
by: George2 | last post by:
Hello everyone, I think the value_type of vector<intshould be int and the value_type of vector<intshould be int*. But I am not 100% sure, 1. how to write a program to verify this idea; 2....
3
by: massysett | last post by:
I'm puzzled about part of the standard. 23.1 states that items stored in a container must be assignable. Therefore, the items in a map--that is, std::pair<const Key, valuemust be assignable....
1
by: antoanish | last post by:
Hello, My Requirement is 1. Copy data from iterator to an array ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. )...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.