473,406 Members | 2,293 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,406 software developers and data experts.

Use of explicit

Consider the following code:

// Start of code

class MyException_t {

public:

// Default constructor.

inline explicit MyException_t () {}

// Initializer constructor.

inline explicit MyException_t (const MyException_t &E) {}

};

int main () { // I know, it needs parameters, that's not my point now.

MyException_t E;
throw E;

}

// End of code

Most compilers have no problem with it, but one compiler shows the following error message:

ppc_60x-g++ -static Except.cpp -o Except
Except.cpp: In function `int main()':
Except.cpp:18: error: no matching function for call to `MyException_t::MyException_t(MyException_t&)'
Except.cpp:18: error: in thrown expression

However, if the "explicit" keywords are remove, it compiles without problem.
Which of the compilers are following the C++ standard?,
The compilers that compile the code with the explicit keyword without error messages,
or the compiler that generates the error messages?

Regards,
Fred.Zwarts.
Mar 2 '06 #1
2 3911
Fred Zwarts wrote:
class MyException_t {
public:
// Default constructor.
inline explicit MyException_t () {}

// Initializer constructor.
inline explicit MyException_t (const MyException_t &E) {}
};
Both of these constructors are only used with "direct-initialization
syntax" or with explicit casts. They are not used to implicitly
create an object as is needed e.g. to copy an exception thrown.

BTW, the more conventional name for your "initializer constructor"
is "copy constructor".
MyException_t E;
throw E;


The throw statement requires a viable copy constructor. However,
your copy constructor requires either an explicit cast or the
use of direct-initialization to be used and is thus not viable.
The use of 'explicit' on a copy constructor is not buying you much
anyway: the copy constructor is not used for any conversions and
the only aspect of 'explicit' is to suppress use of constructors
for implicit conversions. Thus, the correct fix is to drop the
'explicit' keyword from your copy constructor.

BTW, EDG front-end based compilers also reject your code.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 2 '06 #2
Dietmar Kuehl wrote:
Fred Zwarts wrote:
class MyException_t {
public:
// Default constructor.
inline explicit MyException_t () {}

// Initializer constructor.
inline explicit MyException_t (const MyException_t &E) {}
};


BTW, EDG front-end based compilers also reject your code.


GCC does too.

Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: biro man | last post by:
I have a dynamic database driven page that I have been working on and tweaking etc until I have got it to work pretty much the way I want it to. I also have a defined website. I wish to include...
2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
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;
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
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....
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...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.