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

conversion ctors question

I am baffled by this behaviour.

I have a class A declared as follows:

class A
{
public:
A();
explicit A(const std::string& value);
explicit A(const TimestampParam& value) ;
explicit A(const long value) ;
explicit A(const double value) ;
explicit A(const bool value) ;
A(const Object& value) ;
A(const A);
A& operator= (const A&);
~A();
}
I am using the class in statements like this:

std::vector<Aparams ;

//field id
A field(100L) ; //requires 'L' specifier else ambigious ctor
params.push_back(field);

//field name
field = A(std::string("Homer")); //ok
params.push_back(field);
!!! PRETZEL LOGIC ALERT !!!

//field descr
field = A("likes donuts"); //calls A::A(const bool)
params.push_back(field);
Why is the compiler casting a std::string to bool? I took of the
explicit keyword so I could automatically convert between the supported
types and A, and still, std::string is being cast as bool - WHY ??? -
and how do I fix this?
Jul 19 '07 #1
6 1806


Grey Alien wrote:
I am baffled by this behaviour.

I have a class A declared as follows:

class A
{
public:
A();
explicit A(const std::string& value);
explicit A(const TimestampParam& value) ;
explicit A(const long value) ;
explicit A(const double value) ;
explicit A(const bool value) ;
A(const Object& value) ;
A(const A);
correction:
A(const A&);
A& operator= (const A&);
~A();
}
I am using the class in statements like this:

std::vector<Aparams ;

//field id
A field(100L) ; //requires 'L' specifier else ambigious ctor
params.push_back(field);

//field name
field = A(std::string("Homer")); //ok
params.push_back(field);
!!! PRETZEL LOGIC ALERT !!!

//field descr
field = A("likes donuts"); //calls A::A(const bool)
params.push_back(field);
Why is the compiler casting a std::string to bool? I took of the
explicit keyword so I could automatically convert between the supported
types and A, and still, std::string is being cast as bool - WHY ??? -
and how do I fix this?
Jul 19 '07 #2
Grey Alien wrote:
I am baffled by this behaviour.

I have a class A declared as follows:

class A
{
public:
A();
explicit A(const std::string& value);
explicit A(const TimestampParam& value) ;
explicit A(const long value) ;
explicit A(const double value) ;
explicit A(const bool value) ;
A(const Object& value) ;
A(const A);
A& operator= (const A&);
~A();
}
I am using the class in statements like this:

std::vector<Aparams ;

//field id
A field(100L) ; //requires 'L' specifier else ambigious ctor
params.push_back(field);

//field name
field = A(std::string("Homer")); //ok
params.push_back(field);
!!! PRETZEL LOGIC ALERT !!!

//field descr
field = A("likes donuts"); //calls A::A(const bool)
params.push_back(field);
Why is the compiler casting a std::string to bool?
Where do you see a string? Why do you think it's casting it to 'bool'?
I took of the
explicit keyword so I could automatically convert between the
supported types and A, and still, std::string is being cast as bool -
WHY ??? - and how do I fix this?
I don't see any std::string cast to bool. Please elaborate.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '07 #3
"Grey Alien" <gr**@andromeda.comwrote in message
news:Zv******************************@bt.com...
>I am baffled by this behaviour.

I have a class A declared as follows:

class A
{
public: A();
explicit A(const std::string& value);
explicit A(const TimestampParam& value) ;
explicit A(const long value) ;
explicit A(const double value) ;
explicit A(const bool value) ;
A(const Object& value) ;
A(const A);
A& operator= (const A&);
~A();
}
I am using the class in statements like this:

std::vector<Aparams ;

//field id
A field(100L) ; //requires 'L' specifier else ambigious ctor
params.push_back(field);

//field name
field = A(std::string("Homer")); //ok
params.push_back(field);
!!! PRETZEL LOGIC ALERT !!!

//field descr
field = A("likes donuts"); //calls A::A(const bool)
params.push_back(field);
Why is the compiler casting a std::string to bool?
"likes donuts" is not a std::string. It is a char array constant, a char
pointer if you will. You have no constructore that takes a char* as a
parameter. It seems the compiler finds bool as the one to use. If you want
a char* to be used as a constructor, then make one.
I took of the explicit keyword so I could automatically convert between
the supported types and A, and still, std::string is being cast as bool -
WHY ??? - and how do I fix this?


Jul 19 '07 #4
Jim Langston wrote:
explicit A(const bool value) ;
>Why is the compiler casting a std::string to bool?

"likes donuts" is not a std::string. *It is a char array constant, a char
pointer if you will. *You have no constructore that takes a char* as a
parameter. *It seems the compiler finds bool as the one to use. *If you
want a char* to be used as a constructor, then make one.
shouldn't the 'explicit' keyword rule out this conversion?
that's what it's meant for, right?

g.

Jul 19 '07 #5
Giacomo Cerrai wrote:
Jim Langston wrote:
>explicit A(const bool value) ;
>>Why is the compiler casting a std::string to bool?

"likes donuts" is not a std::string. It is a char array constant, a
char pointer if you will. You have no constructore that takes a
char* as a parameter. It seems the compiler finds bool as the one to
use. If you want a char* to be used as a constructor, then make one.

shouldn't the 'explicit' keyword rule out this conversion?
that's what it's meant for, right?
'explicit' only concerns the conversion from the type of the argument
of your constructor into your type. You cannot control the language
standard conversions that way. The implicit conversion between
a pointer to an object and bool exists and cannot be removed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '07 #6
On Jul 19, 4:42 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Grey Alien" <g...@andromeda.comwrote in message
news:Zv******************************@bt.com...
I am baffled by this behaviour.
I have a class A declared as follows:
class A
{
public: A();
explicit A(const std::string& value);
explicit A(const TimestampParam& value) ;
explicit A(const long value) ;
explicit A(const double value) ;
explicit A(const bool value) ;
A(const Object& value) ;
A(const A);
A& operator= (const A&);
~A();
}
I am using the class in statements like this:
std::vector<Aparams ;
[...]
//field descr
field = A("likes donuts"); //calls A::A(const bool)
params.push_back(field);
Why is the compiler casting a std::string to bool?
"likes donuts" is not a std::string. It is a char array
constant, a char pointer if you will. You have no
constructore that takes a char* as a parameter. It seems the
compiler finds bool as the one to use. If you want a char* to
be used as a constructor, then make one.
You mean a char const* constructor, don't you?

The compiler is required to follow the rules of the language.
The language says that there is an implicit conversion of char
const* to bool, that the conversion of char const* to
std::string is a user defined conversion, and that implicit
conversions are to be prefered over user defined conversions.

My understanding is that the intent was that compilers would
warn about such implicit conversions; that they were present
simply to avoid breaking existing code. I don't know of any
compiler which does, however.

In the meantime, it's worth repeating a simple rule concerning
overloading: anytime you overload a function, make sure that
there is an overload for the type a constant value will
typically have. If you overload for any numeric type, for
example, ensure that you also overload for int. (Note that
A(42) will be ambiguous with the original code.) If you
overload for any floating point type, ensure that you also
overload for double. (If you only overload for int and float,
A(1.3) would be ambiguous.) And if you overload for std::string
(or any other class which would typically convert automatically
from a string literal), ensure that you also overload for char
const*. Note too in this respect that bool is an integral (and
thus a numeric) type.

If you're coding guidelines doesn't have a rule along these
lines, add it now.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 20 '07 #7

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

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
6
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
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...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
1
by: Belebele | last post by:
I would like to have the compiler bind a Base class const reference to a temporary object of a Derived class when calling another class constructor. Please see the code below: class Base {}; ...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.