473,405 Members | 2,294 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,405 software developers and data experts.

value_type of back_inserter?


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 question is, does the standard dictate that the value_type of a
back_inserter is 'void'? If so, why? I would expect the value_type of a
back_inserter to be the same as the value_type of the container it wraps.
template < typename It >
void foo( It it ) {
typename iterator_traits<It>::value_type bar;
cin >> bar;
*it++ = bar;
}

int main() {
vector<int> vec( 1 );
foo( vec.begin() ); // (1)
cout << vec.front();

vector<double> vec2;
foo( back_inserter( vec2 ) ); // (2)
cout << vec2.front();
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 31 '06 #1
6 3021
The source code in stl_iterator.h:

template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
....

The output iterator means write only.

Mar 31 '06 #2
In article <11**********************@z34g2000cwc.googlegroups .com>,
"As******@gmail.com" <As******@gmail.com> wrote:
The source code in stl_iterator.h:

template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
...

The output iterator means write only.


Is this then dictated by the standard? Why would the value_type of an
output iterator be void?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 31 '06 #3
Daniel T. wrote:
In article <11**********************@z34g2000cwc.googlegroups .com>,
"As******@gmail.com" <As******@gmail.com> wrote:
The source code in stl_iterator.h:

template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
...

The output iterator means write only.
Is this then dictated by the standard?


Yes, in the sense that it is permissible under the standard that an
output iterator be write-only.
Why would the value_type of an
output iterator be void?


Because that's the essence of an output iterator - you can send a value
to it, but you can't necessarily read a value from it.

Take a look at the SGI STL reference on output iterators:

http://www.sgi.com/tech/stl/Iterators.html
http://www.sgi.com/tech/stl/OutputIterator.html

Best regards,

Tom

Mar 31 '06 #4
In article <11*********************@u72g2000cwu.googlegroups. com>,
"Thomas Tutone" <Th***********@yahoo.com> wrote:
Daniel T. wrote:
In article <11**********************@z34g2000cwc.googlegroups .com>,
"As******@gmail.com" <As******@gmail.com> wrote:
The source code in stl_iterator.h:

template<typename _Container>
class back_insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
...

The output iterator means write only.


Is this then dictated by the standard?


Yes, in the sense that it is permissible under the standard that an
output iterator be write-only.
Why would the value_type of an
output iterator be void?


Because that's the essence of an output iterator - you can send a value
to it, but you can't necessarily read a value from it.

Take a look at the SGI STL reference on output iterators:

http://www.sgi.com/tech/stl/Iterators.html
http://www.sgi.com/tech/stl/OutputIterator.html


Thank you, the specific comments about Output Iterators above was
helpful. Especially the comment: "...for an Output Iterator, in the
expression *x = t, there is no reason why operator= must take a unique
type."
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 31 '06 #5

Daniel T. wrote:
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 question is, does the standard dictate that the value_type of a
back_inserter is 'void'? If so, why? I would expect the value_type of a
back_inserter to be the same as the value_type of the container it wraps.
template < typename It >
void foo( It it ) {
typename iterator_traits<It>::value_type bar;
cin >> bar;
*it++ = bar;
}

int main() {
vector<int> vec( 1 );
foo( vec.begin() ); // (1)
cout << vec.front();

vector<double> vec2;
foo( back_inserter( vec2 ) ); // (2)
cout << vec2.front();
}


As a workaround for already mentioned issues you could use the
following trick:

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

struct S
{
S const& self_const() { return *this; }

// false convesion, never used
template<class C>
operator back_insert_iterator<C>&();

template<class T>
operator T() const
{
T t;
cin >> t;
return t;
}
};

template < typename It >
void foo( It it )
{
*it = S().self_const();
}

int main() {
vector<int> vec( 1 );
foo( vec.begin() ); // (1)
cout << vec.front();

vector<double> vec2;
foo( back_inserter( vec2 ) ); // (2)
cout << vec2.front();

}

I'm not sure if the standard guarantees it must work. g++ 4.1 compiles
it fine, comeau online does not. You may also need to add more false
conversion functions to circumvent overload resolution should you
decide to use the trick.

P.S. Just wasted 30 minutes for fun thinking about the problem. Would
not use in production code as tricky code as this piece. Keep it
simple...

Mar 31 '06 #6
Daniel T. wrote:

Is this then dictated by the standard? Why would the value_type of an
output iterator be void?


What else can it be? You can assign to *iter, but you can't look at it.
So the value_type isn't actually meaningful, and void seems to be the
best analog of no-meaningful-type. Output iterators do have the notion
of types that are "writable to the ... iterator", but that's much
broader than a value type, and there's no general notation in C++ for
representing a set of types.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 1 '06 #7

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: Mark A. Gibbs | last post by:
I have a string conversion function that looks something like this (apologies, but I cannot post the actual code): whar_t char_to_wchar(char); wstring to_wstring(const string& s) { wstring...
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...
3
by: silverburgh.meryl | last post by:
i have the following code, but it does not compile because I pass in back_inserter(b) as the output iterator in the remove_copy_if algorithm. My questions are: 1. why it does not compile? Passing...
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...
17
by: Jess | last post by:
Hello, The iterator adaptor "back_inserter" takes a container and returns a iterator so that we can insert elements to the end of the container. Out of curiosity, I tried to look at what element...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.