By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Accessing data members from templated copy constructor

P: n/a
Hi all,

I can't compile the following code with Gcc 3.3 (compiles with
CodeWarrior 8):

template <typename T = int>
class Boule
{
friend class Boule<int>;
friend class Boule<float>;
protected:
T _data; // Line #7
inline Boule() {}
template <typename U>
inline Boule(const Boule<U>& b) : _data(b._data) {} // Line # 10
};

template <typename T = int>
class Rouston : protected Boule<T>
{
public:
inline Rouston() {}
template <typename U>
inline Rouston(const Rouston<U>& r) : Boule<T>(static_cast<const
Boule<U>&>(r)) {}
};

The compiler reports the following errors:

Test-2.cpp:7: error: `int Boule<int>::_data' is protected
Test-2.cpp:10: error: within this context

I suspect a bug in the compiler... Is there any workaround ?

Any help welcome !

-------------------------------------------------------------------------
Alexandre Tolmos
E-mail: kt***@free.fr
ICQ: 92964905
-------------------------------------------------------------------------
"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn."
-------------------------------------------------------------------------

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Alexandre Tolmos" <kt***@free.fr> wrote...
Sorry, I forgot to include the main function:
[...]


If you compile your program:
----------------------------------------
template <typename T = int>
class Boule
{
friend class Boule<int>;
friend class Boule<float>;
protected:
T _data; // Line #7
inline Boule() {}
template <typename U>
inline Boule(const Boule<U>& b) : _data(b._data) {}
};

template <typename T = int>
class Rouston : protected Boule<T>
{
public:
Rouston() {}
template <typename U>
Rouston(const Rouston<U>& r)
: Boule<T>(static_cast<const Boule<U>&>(r)) {} // 27
};

int main(int, char*[])
{
Rouston<> r1;
Rouston<float> r2(r1); // 33
return 0;
}
-----------------------------------------------
using Comeau C++, you get more meaningful diagnostic:

"ComeauTest.c", line 27: error: conversion to inaccessible base class
"Boule<int>"
is not allowed
: Boule<T>(static_cast<const Boule<U>&>(r)) {}
^
detected during instantiation of "Rouston<T>::Rouston(const
Rouston<U> &) [with T=float, U=int]" at line 33
------------------------------------------------

As you can see, you're trying to convert Rouston<int> to Boule<int>.
That conversion is not accessible to Rouston<float>.

Victor
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.