🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4108

Are these "implicit" casts legal?

#4108
From: Angelo Salvade
Date: Fri, 19 Apr 1996 14:43
80 lines
2327 bytes
Can anybody tell me if the following code is legal:

//----------------------------------------------------------------

class A {};

class B: public A {};

//----------------------------------------------------------------

// with templates

template <class O>
class Ref {
	public:
		Ref();
		Ref(O* value);
		operator O*();
};

extern void f1(Ref<A> a);

static void t1()
{
	Ref<B> b;
	Ref<A> a(b); // #1

	f1(b); // #2
}

//----------------------------------------------------------------

// without templates

class RefA {
	public:
		RefA(A* value);
};

class RefB {
	public:
		RefB();
		operator B*();
};

extern void f2(RefA a);

static void t2()
{
	RefB b;
	RefA a(b); // #3

	f2(b); // #4
}

//----------------------------------------------------------------

I believe that #1-4 should work identically and are correct C++:
A Ref<A> should be initialised with a Ref<B>.
The following steps should happen:
- call of Ref<B>.operator B*()
- C++ built in conversion of B* to A*
- call of constructor Ref<A>(A*)

I checked this code with the Metrowerks CW8,
GNU 2.7.2 and SunPro C++ 4.0.1 & C++ 4.1 compilers.  
The SunPro C++ 4.1 is the only compiler of those 
that didn't compile #2 and #4. It gave the following message:
"Error: Cannot use Ref<B> to initialize Ref<A>".

Is my code illegal or is this a bug in the compiler?

Angelo Salvade
salvade@ecofin.ch
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <00614.2912664663.294@ecofin.uucp>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
From: saa@ecofin.ch (Angelo Salvade)
Message-ID: <00614.2912664663.294@ecofin.uucp>
X-Original-Date: Thu, 18 Apr 1996 9:30:03 -0100
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 19 Apr 96 14:43:42 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
X-Umcp-Priority: 1
Subject: Are these "implicit" casts legal?
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMXem6eEDnX0m9pzZAQGSFwF/UuBIB5ms4HHVdDsyxxcILLKlyRuyK4U4
 QrSkrapo/zvZkTbnD0YFmFOfBVHZX4ry
 =eg4z