473,382 Members | 1,365 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,382 software developers and data experts.

vector ctor passed with two arguments of same type

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<intv(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class Invector<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<intv(10, 50);

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

return EXIT_SUCCESS;
}

Thanks
V.Subramanian
Jun 27 '08 #1
5 1552
su**************@yahoo.com, India wrote:
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<intv(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class Invector<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<intv(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
Jun 27 '08 #2
su**************@yahoo.com, India wrote:
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<intv(10, 50); // (size, value) or (iterator1, iterator2)?

The problem is that the declaration

template <class Invector<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<intv(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
Jun 27 '08 #3
On Apr 29, 9:47 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
subramanian10...@yahoo.com, India wrote:
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<intv(10, 50); // (size, value) or (iterator1, iterator2)?
The problem is that the declaration
template <class Invector<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<intv(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.
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
* Barry <dhb2...@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
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<intv(10, 50); // (size, value) or (iterator1, iterator2)?
The problem is that the declaration
template <class Invector<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.
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
Jun 27 '08 #5
su**************@yahoo.com wrote:
* Barry <dhb2...@gmail.comwrote:
>subramanian10...@yahoo.com, India wrote:
>>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<intv(10, 50); // (size, value) or (iterator1,
iterator2)?
>>The problem is that the declaration
>>template <class Invector<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.

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


Jun 27 '08 #6

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

Similar topics

5
by: Johann Gerell | last post by:
I have this iterator: class CFileIterator : public std::iterator<std::input_iterator_tag, const std::wstring> { public: CFileIterator(const std::wstring& wildcardPath); .... };
3
by: Pelle Beckman | last post by:
Hi all, I have a few - beginners - questions: * Why can't I put object references in a std::vector, i.e std::vector<MyClass&> ? At least in doesnt work in gcc (mingw, win32) * What's the...
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
11
by: Chris Dams | last post by:
Dear all, I found out that the program #include<vector> using namespace std; int main() { vector<int*> v(2,0);
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
11
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <vector> using namespace std; class Test { public:
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
29
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.