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