Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

In C++ dynamic polymorphism (virtual) is implemented by vptr and vtables. In the same line, can I know how explicit keyword (used for constructor and copy constructor) is implemented?

Yuvaraj
Posted
Updated 31-May-10 21:29pm
v2

There's no real runtime implementation needed. It's the compiler which can do the job: if a constructor has been marked as explicit, the compiler simply doesn't allow implicit conversions. The compiler does an implicit conversion for you when such a case is encountered. So, it can also perfectly not do that conversion if the constructor is marked as explicit.
 
Share this answer
 
Comments
Yuvaraj Gogoi 1-Jun-10 2:52am    
So its the compiler's job to mark constructors as explicit if given.
Thanks for your answer!
explicit just tells the compiler to "don't use that constructor to translate implicit conversion".

In case an implicit conversion is required, the compiler will simply look for another suitable function (like a Class::operator Type())

The function itself is not "different". no so effert will go in the translated code.

Note that the explicit keyword has a clue only in constructor that can take only one argument, since are the ones used in conversions.
 
Share this answer
 
v2
Comments
Yuvaraj Gogoi 3-Jun-10 2:16am    
I am not getting what your last line tells "has a clue only in constructor taking one argument, since are the ones used in conversions".
If we provide constructor with more than one arguement and with default arguements,will the keyword explicit work?

Thanks a lot for your answer!
Emilio Garavaglia 3-Jun-10 3:56am    
Better edited: "that can take only one argument".
Providing more arguments, with all defaults (or with all defaults but the first) in fact let the possibility to call it with one argument available.
Yuvaraj Gogoi 3-Jun-10 4:21am    
So providing multiple default arguments for all, but the first will work with explicit. Am I clear?
Thanks again for the input!
Emilio Garavaglia 3-Jun-10 11:08am    
yes.
Yuvaraj Gogoi 4-Jun-10 4:17am    
Thanks for the clarification!
It's implemented somewhere in the compilers front end, it doesn't get anywhere near the code generator and I'm fairly sure it doesn't play any part in overload resolution.

You can see this by defining two different versions of a class in two different translation units, the only difference being the use of explicit. So in one:

class A
{
    public:
        explicit A();
};


and in the other:

class A
{
    public:
        A();
};


and use both definitions of the class in their respective translation units.

You can then implement both versions in a third translation unit:

class A
{
    public:
        A();
};

A::A()
{
}

and hey presto the implementation will satisfy both explicit and non-explicit uses in either of the other two translation units. Incidentally you can do the same trick with public, protected and private.

So what does this mean? As far as I can tell it means no information about whether a constructor is marked explicit ends up in the object file along with it's implementation.

I may be wrong though, your compiler may vary. Don't rely on it in production code.

Cheers,

Ash
 
Share this answer
 
Comments
Yuvaraj Gogoi 1-Jun-10 3:46am    
So implementation of explicit is primarily the compilers job.
Thanks for the answer...
Aescleal 1-Jun-10 7:02am    
The implementation of C++ is generally the compiler's job. In the case of explicit and protection specifications it doesn't do anything to enforce the constraints across translation units, unlike, say overload resolution.
Yuvaraj Gogoi 3-Jun-10 2:18am    
thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900