Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > conversion ctors question

Reply
Thread Tools

conversion ctors question

 
 
Grey Alien
Guest
Posts: n/a
 
      07-19-2007
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<A> params ;

//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?
 
Reply With Quote
 
 
 
 
Grey Alien
Guest
Posts: n/a
 
      07-19-2007


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<A> params ;
>
> //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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2007
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<A> params ;
>
> //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


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      07-19-2007
"Grey Alien" <> wrote in message
news:...
>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<A> params ;
>
> //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?




 
Reply With Quote
 
Giacomo Cerrai
Guest
Posts: n/a
 
      07-19-2007
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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2007
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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 4:42 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Grey Alien" <g...@andromeda.com> wrote in message
> news:...


> >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<A> params ;


[...]
> > //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:
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Named CTORs - symualting it by tempaltes? Raf256 C++ 7 06-02-2006 01:44 AM
Empty classes, default ctors and const objects? BigMan C++ 4 06-25-2005 09:50 PM
Calling member function in ctors and dtors. BigMan@abv.bg C++ 6 02-04-2005 02:48 PM
Problem with template ctors (smart pointer) Carsten Spieß C++ 4 06-17-2004 02:16 PM
Explicit ctors Dave C++ 0 11-26-2003 09:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57