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

explicit ctor in vector

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
5 1785
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave | last post by:
Hello all, 1. I am able to declare as explicit ctors that cannot take exactly one argument - they either take fewer (default ctor) or more arguments. What is the significance of declaring such...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
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); .... };
10
by: richardclay09 | last post by:
The output of this: #include <iostream> #include <vector> using namespace std; struct X { int i; X(const X& x) : i(x.i) { cout << "ctor copy: " << i << endl;
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...
9
by: D.J. Stachniak | last post by:
Hi, I have a question about explicit constructors and the C++ standard. Forgive me for my laziness in not pouring over the spec myself, but I thought I'd ask first and read later. I have a...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
10
by: jlongstreet | last post by:
Please correct any misconceptions, or voice concerns about this being a stupid idea in general. I was wondering today: why doesn't C++ have explicit typedefs? explicit typedef unsigned int...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.