Accessing data members from templated copy constructor

  • Thread starter Alexandre Tolmos
  • Start date
A

Alexandre Tolmos

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 !
 
V

Victor Bazarov

Alexandre Tolmos said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top