how to call conversion operator

1 view
Skip to first unread message

Tony

unread,
Dec 5, 2003, 5:36:13 PM12/5/03
to
Hi, C++ Gurus,

struct A { };

struct B {
operator A() const { return A(); }
};

int main()
{
A a;
B b;

a = (B)b;
a = b.operator A();

return 0;
}

/* (B)b is C style cast, it is ugly -- I was told.
b.operator A() does not look good.
other than above two ways to call operator A(),
are there other ways to call operator A() explicitly?
thanks. */

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Ralf

unread,
Dec 6, 2003, 5:26:50 AM12/6/03
to
> struct A { };
>
> struct B {
> operator A() const { return A(); }
> };
>
> int main()
> {
> A a;
> B b;
>
> a = (B)b;
I think, you mean a = (A)b;

> a = b.operator A();
>
> return 0;
> }
>
> /* (B)b is C style cast, it is ugly -- I was told.
> b.operator A() does not look good.
> other than above two ways to call operator A(),
> are there other ways to call operator A() explicitly?
> thanks. */

Yes!

int main()
{
A a;
B b;

a = (A)b;
a =A(B);
a=static_cast<A>(b);

return 0;
}


www.oop-trainer.de

Steve Dewhurst

unread,
Dec 7, 2003, 10:01:39 AM12/7/03
to
tongr...@yahoo.com (Tony) wrote in message news:<1f87458d.03120...@posting.google.com>...

> Hi, C++ Gurus,
>
> struct A { };
>
> struct B {
> operator A() const { return A(); }
> };
>
> int main()
> {
> A a;
> B b;
>
> a = (B)b;
> a = b.operator A();
>
> return 0;
> }
>
> /* (B)b is C style cast, it is ugly -- I was told.
> b.operator A() does not look good.
> other than above two ways to call operator A(),
> are there other ways to call operator A() explicitly?
> thanks. */

Of course you can use one of the new cast operators, like static_cast,
but I think the preferred conversion is the implicit one; just assign
b to a and let the compiler determine how to perform the conversion.
After all, that's why the designer of class B declared a conversion
operator instead of something simpler.

If you're looking for an explicit conversion because use of
B::operator A is ambiguous or confusing, that's an indication that you
shouldn't be using a conversion operator at all in B, but should
instead provide an explicit conversion function like B::toA. This is
typically preferable to use of a conversion operator.

Steve
---
Stephen C. Dewhurst
http://www.semantics.org

David Abrahams

unread,
Dec 7, 2003, 5:48:12 PM12/7/03
to
tongr...@yahoo.com (Tony) writes:

> Hi, C++ Gurus,
>
> struct A { };
>
> struct B {
> operator A() const { return A(); }
> };
>
> int main()
> {
> A a;
> B b;
>
> a = (B)b;
> a = b.operator A();
>
> return 0;
> }
>
> /* (B)b is C style cast, it is ugly -- I was told.
> b.operator A() does not look good.
> other than above two ways to call operator A(),
> are there other ways to call operator A() explicitly?
> thanks. */

You might try:


a = boost::implicit_cast<A>(b);

implicit_cast is defined roughly like this:

template <class T>
struct identity { typedef T type; };

// implementation originally suggested by C. Green in
// The use of identity creates a non-deduced form, so that the
// explicit template argument must be supplied
template <typename T>
inline T implicit_cast (typename identity<T>::type x) {
return x;
}

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Peter Koch Larsen

unread,
Dec 8, 2003, 5:51:06 AM12/8/03
to

"Tony" <tongr...@yahoo.com> skrev i en meddelelse
news:1f87458d.03120...@posting.google.com...

> Hi, C++ Gurus,
>
> struct A { };
>
> struct B {
> operator A() const { return A(); }
> };
>
> int main()
> {
> A a;
> B b;
>
> a = (B)b;
> a = b.operator A();

Why not simply:
a = b;
?

Jonathan Turkanis

unread,
Dec 8, 2003, 6:02:01 PM12/8/03
to

"Peter Koch Larsen" <p...@mailme.dk> wrote in message
news:3fd315c6$0$9730$edfa...@dread14.news.tele.dk...

> > int main()
> > {
> > A a;
> > B b;
> >
> > a = (B)b;
> > a = b.operator A();
>
> Why not simply:
> a = b;
> ?
> >

A could have an assignment operator taking a B. There is also a similar
problem if A has a non-explicit constructor taking a B, and one writes

A a = b;

Jonathan

Francis Glassborow

unread,
Dec 9, 2003, 11:55:33 AM12/9/03
to
In article <br2ho5$27so3p$1...@ID-216073.news.uni-berlin.de>, Jonathan
Turkanis <tech...@kangaroologic.com> writes

>"Peter Koch Larsen" <p...@mailme.dk> wrote in message
>news:3fd315c6$0$9730$edfa...@dread14.news.tele.dk...
>
> > > int main()
> > > {
> > > A a;
> > > B b;
> > >
> > > a = (B)b;
> > > a = b.operator A();
> >
> > Why not simply:
> > a = b;
> > ?
> > >
>
>A could have an assignment operator taking a B. There is also a similar
>problem if A has a non-explicit constructor taking a B, and one writes
>
> A a = b;

I think if there exists A::A(B&), A::A(B) etc. then we are always going
to have a problem if B provides an operator A() because you now have two
ways to get an A from a B.

For example
a = b;

can be
a = b.operator A();
or
a = A(b);

I just tried the following with MinGW 3.2.3:

struct B;

struct A {
A(B&){}; // #1
A(){};
};


struct B {
operator A(){return A();} // #2
};


int main(){
A a;
B b;

a = b;
}

I was expecting to get ambiguity but I got a rather more elusive
message:

19 no match for 'A&=B&'operator
candidates are A& A::operator=(const A&) <near match>

The code compiles if either of #1 or #2 is commented out which
looks as if the compiler is really finding it ambiguous but does
not say so.


--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.

Jonathan Turkanis

unread,
Dec 9, 2003, 5:10:56 PM12/9/03
to

"Francis Glassborow" <fra...@robinton.demon.co.uk> wrote in message news:

> >A could have an assignment operator taking a B. There is also a similar
> >problem if A has a non-explicit constructor taking a B, and one writes
> >
> > A a = b;
>
> I think if there exists A::A(B&), A::A(B) etc. then we are always going
> to have a problem if B provides an operator A() because you now have two
> ways to get an A from a B.
>
> For example
> a = b;
>
> can be
> a = b.operator A();
> or
> a = A(b);
>
> I just tried the following with MinGW 3.2.3:
>
<snip>

>
> I was expecting to get ambiguity but I got a rather more elusive
> message:
>
> 19 no match for 'A&=B&'operator
> candidates are A& A::operator=(const A&) <near match>
>
> The code compiles if either of #1 or #2 is commented out which
> looks as if the compiler is really finding it ambiguous but does
> not say so.
>
>

Very interesting. Before posting, I tried out a number of these scenarios on
MinGW 3.2, Comeau 4.3.3
and VC7.1 and got a wide variety of divergent complaints of ambiguity as
well as some interesting resolutions.

However, b.operator A() always works. Maybe a template would be prettier.
E.g.:

template<typename Target, typename Source>
inline Target conversion_operator(Source& src) { return src.operator
Target(); }

a = conversion_operator<A>(b);


Originally, I was going to suggest what the standard behavior should be in
the difficult cases. But I 'm not sure, so I dediced just to say 'there is a
problem' :-)

Jonathan

Reply all
Reply to author
Forward
0 new messages