Accessing data members from templated copy constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandre Tolmos

    Accessing data members from templated copy constructor

    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>::_da ta' 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: ktulu@free.fr
    ICQ: 92964905
    -------------------------------------------------------------------------
    "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn."
    -------------------------------------------------------------------------

  • Victor Bazarov

    #2
    Re: Accessing data members from templated copy constructor

    "Alexandre Tolmos" <ktulu@free.f r> wrote...[color=blue]
    > Sorry, I forgot to include the main function:
    > [...][/color]

    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>::Ro uston(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


    Comment

    Working...