explicit ctor in vector

S

subramanian100in

In the standard library vector, the ctor
explicit vector::vector(size_type __n)

is declared as explicit. I am unable to get
the reason for it being declared as explicit.
Kindly clarify with an example.

Does the standard require that this ctor should be
declared explicit ?

Also, please let me know if there are any guidelines
for identifying which 'one argument ctors' need to be
declared as explicit.

Thanks
V.Subramanian
 
Z

Zeppe

In the standard library vector, the ctor
explicit vector::vector(size_type __n)

is declared as explicit. I am unable to get
the reason for it being declared as explicit.
Kindly clarify with an example.

because otherwise it would allow for implici cast. For Example:

vector<int> v1 = 3;

you may want this to be an error (in fact, the above statement is quite
obscure and doesn't make much sense logically). It will be so if the
constructor is declared as explicit.
Does the standard require that this ctor should be
declared explicit ?
yes.


Also, please let me know if there are any guidelines
for identifying which 'one argument ctors' need to be
declared as explicit.

common sense, afaik. When you have a constructor
Foo(const X& x);
you have to ask to yourself: can the object x of class X be (implicitly)
converted into a Foo object? Only if the object is yes you can avoid
putting the explicit keyword.

Regards,

Zeppe
 
M

Marco Manfredini

In the standard library vector, the ctor
explicit vector::vector(size_type __n)

is declared as explicit. I am unable to get
the reason for it being declared as explicit.
Kindly clarify with an example.

If vector(size_type __n) wasn't explicit, then this would be valid code:

vector<SomeType> x;
....
if (x==4)
{
x=0;
}

Quite unlikely that you want "4" as a possible shorthand for a vector of
four elements.
 
R

Ron Natalie

is declared as explicit. I am unable to get
the reason for it being declared as explicit.
Kindly clarify with an example.

Because they didn't want integers implicitly converted to
vectors.
Does the standard require that this ctor should be
declared explicit ?
Absolutely.


Also, please let me know if there are any guidelines
for identifying which 'one argument ctors' need to be
declared as explicit.
The ones in the standard that say they are explicit MUST
be explicit.

As for your own, anytime you don't want the constuctor
to implicitly convert to that type, you need to make it
explicit. A numeric arg is highly suspect. It's also
quite common to not want either char* or string arguments
to convert implicitly.
 
T

Tadeusz B. Kopec

On Fri, 23 Nov 2007 09:32:28 -0500, Ron Natalie wrote:
[snip]
As for your own, anytime you don't want the constuctor to implicitly
convert to that type, you need to make it explicit. A numeric arg is
highly suspect. It's also quite common to not want either char* or
string arguments to convert implicitly.

I would rather say: you should make the constructor explicit unless you
want the conversion. And think twice before you decide you want a
conversion.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top