Can't initialize inner class template

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm trying to make this compile...

----------------------------------------------------
template <typename T>
class A {
public:
    class InnerA {
    public:
        InnerA(T var) : m_var(var) { }

        InnerA(const InnerA& inner) : m_var(inner.m_var) { }

        template <typename U>
        InnerA(const typename A<U>::InnerA& inner) : m_var(inner.m_var) {
        }

        template <typename U>
        void copy(const typename A<U>::InnerA& inner) {
            m_var = inner.m_var;
        }

    private:
        T m_var;

    template <typename U>
    friend class A::InnerA;
    };

    A(T var) : m_inner(var) { }

    A(const A& obj) : m_inner(obj.m_inner) { }

    template <typename U>
    A(const A<U>& obj) : m_inner(obj.m_inner) { }

    template <typename U>
    void copy(const A<U>& obj) {
        m_inner.copy<U>(obj.m_inner);
    }

private:
    InnerA m_inner;

template <typename U>
friend class A;
};


template <typename T>
class B {
public:
    B(T var) : m_var(var) { }

    B(const B& obj) : m_var(obj.m_var) { }

    template <typename U>
    B(const B<U>& obj) : m_var(obj.m_var) { }

private:
    T m_var;

template <typename U>
friend class B;
};


int main(int argc, char* argv[]) {
    B<int> b1(40);
    B<long> b2(b1);

    A<int> a1(70);
    A<long> a2(9);

    a2.copy(a1);

    A<long> a3(a1);

    return 0;
}
----------------------------------------------------

The compile error I get with gcc 3.3.2 are

inner.cpp: In constructor `A<T>::A(const A<U>&) [with U = int, T = long
int]':
inner.cpp:72:   instantiated from here
inner.cpp:30: error: no matching function for call to `A<long
int>::InnerA::
   InnerA(const A<int>::InnerA&)'
inner.cpp:8: error: candidates are: A<T>::InnerA::InnerA(const
A<T>::InnerA&) 
   [with T = long int]
inner.cpp:6: error:                 A<T>::InnerA::InnerA(T) [with T =
long int]

As you can see, the call to A<T>::InnerA::copy works on line 34, but only
if the function template is explicit.  This would explain why I get the
error when I call the InnerA constructor on line 30.

1) Why can't the compiler deduce the information?  Is this a bug, or am I
missing something in regards to the standard?

2) Is there any way possible (i.e., a trick) that I can keep the InnerA
class an nested class and still invoke the same m_inner constructor in
the initializer list for A<T>(const A<U>&)?

Thanks!

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux