vector ctor passed with two arguments of same type

S

subramanian100in

I am copying the following text as it is from Stroustrup's TC++PL 3rd
edition, page 450.

It says:

"Note that a constructor given two arguments of the same type can be a
match for more than one constructor. For example,

vector<int> v(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class In> vector<In first, In last, const A& = A());

doesn't actually say that 'In' must be an input iterator. The
declaration specifies only that the constructor's two first arguments
must be of the same type. The unfortunate consequence is that v's
declaration causes compile-time or link-time errors."

My Doubt:
---------------
I ran the following program with v(10, 50). It didn't give any
compilation or linker error. Instead it printed ten 50s. I am using g+
+ 3.4.3

Is it wrong with the compiler or my understanding that 'In' should be
an input iterator is wrong. ? Kindly clarify.

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v(10, 50);

for (vector<int>::const_iterator cit = v.begin(); cit !=
v.end(); ++cit)
cout << *cit << endl;

return EXIT_SUCCESS;
}

Thanks
V.Subramanian
 
K

Kai-Uwe Bux

I am copying the following text as it is from Stroustrup's TC++PL 3rd
edition, page 450.

It says:

"Note that a constructor given two arguments of the same type can be a
match for more than one constructor. For example,

vector<int> v(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class In> vector<In first, In last, const A& = A());

doesn't actually say that 'In' must be an input iterator. The
declaration specifies only that the constructor's two first arguments
must be of the same type. The unfortunate consequence is that v's
declaration causes compile-time or link-time errors."

My Doubt:
---------------
I ran the following program with v(10, 50). It didn't give any
compilation or linker error. Instead it printed ten 50s. I am using g+
+ 3.4.3

Is it wrong with the compiler or my understanding that 'In' should be
an input iterator is wrong. ? Kindly clarify.

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v(10, 50);

for (vector<int>::const_iterator cit = v.begin(); cit !=
v.end(); ++cit)
cout << *cit << endl;

return EXIT_SUCCESS;
}

This is covered by the standard [23.1.1/9]:

For every sequence defined in this clause and in clause 21:
? the constructor

template <class InputIterator>
X(InputIterator f, InputIterator l, const Allocator& a = Allocator())

shall have the same effect as:

X(static_cast<typename X::size_type>(f),
static_cast<typename X::value_type>(l),
a)

if InputIterator is an integral type.

...


Best

Kai-Uwe Bux
 
B

Barry

I am copying the following text as it is from Stroustrup's TC++PL 3rd
edition, page 450.

It says:

"Note that a constructor given two arguments of the same type can be a
match for more than one constructor. For example,

vector<int> v(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class In> vector<In first, In last, const A& = A());

doesn't actually say that 'In' must be an input iterator. The
declaration specifies only that the constructor's two first arguments
must be of the same type. The unfortunate consequence is that v's
declaration causes compile-time or link-time errors."

I checked my TC++PL 3rd Special Edition,
I seems quite different from your quote.
My Doubt:
---------------
I ran the following program with v(10, 50). It didn't give any
compilation or linker error. Instead it printed ten 50s. I am using g+
+ 3.4.3

Is it wrong with the compiler or my understanding that 'In' should be
an input iterator is wrong. ? Kindly clarify.

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v(10, 50);

for (vector<int>::const_iterator cit = v.begin(); cit !=
v.end(); ++cit)
cout << *cit << endl;

return EXIT_SUCCESS;
}

I think the rule here is that
non-template function is more viable than template function.
see 10.3
 
J

James Kanze

This is covered by the standard [23.1.1/9]:
For every sequence defined in this clause and in clause 21:
? the constructor
template <class InputIterator>
X(InputIterator f, InputIterator l, const Allocator& a = Allocator())
shall have the same effect as:
X(static_cast<typename X::size_type>(f),
static_cast<typename X::value_type>(l),
a)
if InputIterator is an integral type.

Two additional remarks:

-- This is a "correction" of the C++ committee to the original
STL. I think it was added toward the end of the
standardization process, and Stroustrup's text may be
talking about an earlier version, where you did have to cast
the first argument to a size_t for the correct constructor
to be called.

-- The "correction" also allows things like:
std::vector< std::vector< int > > v( 10, 50 ) ;
because the "effect" is described using an explicit
conversion (static_cast) rather than an implicit one. The
consensus of the committe is that this was more than was
intended, and the next version of the standard has been
reworded so that the "effect" depends on an implicit
conversion (and the preceding line is illegal). (On the
other hand, the new wording will allow something like:
std::vector< double > v( 3.5, 5.0 ) ;
which is currently still illegal.)
 
S

subramanian100in

* Barry said:
I checked my TC++PL 3rd Special Edition,
I seems quite different from your quote.

I am using TC++PL 3rd Edition. You are using TC++PL Special 3rd
edition. I do not have TC++PL SPECIAL 3rd edition. There may be some
differences between these two editions.

Thanks
V.Subramanian
 
B

Bo Persson

I am using TC++PL 3rd Edition. You are using TC++PL Special 3rd
edition. I do not have TC++PL SPECIAL 3rd edition. There may be some
differences between these two editions.

It is actually 3rd Edition and Special Edition (with extra chapters),
not a combination. However, there are 15 to 20 printings of each, with
different "corrections" to the text and the language.

http://www.research.att.com/~bs/3rd_errata.html

I have the 5th printing of the 3rd edition :), where it says that the
library should just make it work.


Bo Persson
 

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,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top