473,804 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::c onst_iterator cit = v.begin(); cit !=
v.end(); ++cit)
cout << *cit << endl;

return EXIT_SUCCESS;
}

Thanks
V.Subramanian
Jun 27 '08 #1
5 1580
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>::c onst_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<t ypename X::size_type>(f ),
static_cast<typ ename 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>::c onst_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>::c onst_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<t ypename X::size_type>(f ),
static_cast<typ ename 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 objektorientier ter Datenverarbeitu ng
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(1 0, 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 "correction s" 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
2419
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
1755
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 difference between passing member inits in the c-tor funtion from
18
2903
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" /////////////////////////////// source code /////////////////////////////// #include<iostream> #include <string> #include<vector> #include<sstream>
11
1794
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
2964
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
11
1411
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <vector> using namespace std; class Test { public:
6
5719
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
29
3438
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 make for much more readable code that is faster to write in some situations. I've not seen this feature documented anywhere
19
10765
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 there an existing adapter? Thanks, Daniel.
0
9706
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
10571
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
10326
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...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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
9143
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...
1
7615
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2990
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.