473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_typ e 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 3045
The source code in stl_iterator.h:

template<typena me _Container>
class back_insert_ite rator
: public iterator<output _iterator_tag, void, void, void, void>
{
....

The output iterator means write only.

Mar 31 '06 #2
In article <11************ **********@z34g 2000cwc.googleg roups.com>,
"As******@gmail .com" <As******@gmail .com> wrote:
The source code in stl_iterator.h:

template<typena me _Container>
class back_insert_ite rator
: 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************ **********@z34g 2000cwc.googleg roups.com>,
"As******@gmail .com" <As******@gmail .com> wrote:
The source code in stl_iterator.h:

template<typena me _Container>
class back_insert_ite rator
: 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************ *********@u72g2 000cwu.googlegr oups.com>,
"Thomas Tutone" <Th***********@ yahoo.com> wrote:
Daniel T. wrote:
In article <11************ **********@z34g 2000cwc.googleg roups.com>,
"As******@gmail .com" <As******@gmail .com> wrote:
The source code in stl_iterator.h:

template<typena me _Container>
class back_insert_ite rator
: 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_typ e 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_ite rator<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
1638
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 Pointer = T*, class Reference = T&> struct iterator;
8
2741
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 result(s.length(), '\0'); transform(s.begin(), s.end(), result.begin(), ptr_fun(char_to_wchar));
8
2071
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 now Defect Report 484 on the standard library. I'm beginning to wonder if I've just misunderstood the purpose of iterator_traits::value_type. I constructed an input iterator where *i returns an int, but I set iterator_traits::value_type to bool....
3
3136
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 a back_inserter() works for copy() algorithm, why not remove_copy_if()? 2. is there a work-around for my problem? I think one solution is to allocate 'b' as big as 'a', but that seems to be a waste of memory for the case of b is a lot smaller...
4
1983
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 then use 'value_type&' and 'const value_type&'. Also, if we define these other types, why not define 'const_value_type', etc....
17
4497
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 the returned iterator refers to. Here is my code: #include<iostream> #include<vector> #include<iterator>
3
2157
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. where to find the definition that the value_type of interator of vector<intis int*.
3
3451
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. However, such a pair--with the const Key--is not assignable. My concern is more than academic; take for instance the following code to make a copy of a map while eliminating some of the elements: #include <map> #include <algorithm>
1
2092
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. ) 2. Copy data from array into iterator ( array should be created dynamically within the function based on data_type of the data held by the iterator /container. ) Implementation ----------------------- //copies data from begin to end of...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.