By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

explicit ctor in vector

P: n/a
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
Nov 23 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
su**************@yahoo.com, India wrote:
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<SomeTypex;
....
if (x==4)
{
x=0;
}

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

--
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());
Nov 23 '07 #2

P: n/a
su**************@yahoo.com, India wrote:
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<intv1 = 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

Nov 23 '07 #3

P: n/a
Zeppe wrote:
converted into a Foo object? Only if the object is yes you can avoid
the *answer* obviously. Sorry for the dyslexia. :)

Regards,

Zeppe
Nov 23 '07 #4

P: n/a
su**************@yahoo.com, India wrote:
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.
Nov 23 '07 #5

P: n/a
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.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
For my birthday I got a humidifier and a de-humidifier... I put them in
the same room and let them fight it out.
-- Steven Wright
Nov 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.