tags:

views:

288

answers:

2

Ive got a problem that if I have a template class, which in turn has a template method that takes a parameter of another instance of the class (with different template arguments), that it can not access protected or private members of the class passed as a parameter, eg:

template<typename T>class MyClass
{
    T v;
public:
    MyClass(T v):v(v){}

    template<typename T2>void foo(MyClass<T2> obj)
    {
        std::cout << v     << " ";
        //error C2248: 'MyClass<T>::v' : cannot access private member declared in class 'MyClass<T>'
        std::cout << obj.v << " ";
        std::cout << v + obj.v << std::endl;
    }
};
int main()
{
    MyClass<int> x(5);
    MyClass<double> y(12.3);
    x.foo(y);
}

Is there someway to say that methods in MyClass<T> have full access to MyClass<SomeOtherT>?

+6  A: 

They are different types: templates construct new types from a template.

You have to make other instantiations of your class friends:

template <typename T>class MyClass
{
    T v;
public:
    MyClass(T v):v(v){}

    template<typename T2>void foo(MyClass<T2> obj)
    {
        std::cout << v     << " ";
        std::cout << obj.v << " ";
        std::cout << v + obj.v << std::endl;
    }

    // Any other type of MyClass is a friend.
    template <typename U>
    friend class MyClass;

    // You can also specialize the above:
    friend class MyClass<int>; // only if this is a MyClass<int> will the
                               // other class let us access its privates
                               // (that is, when you try to access v in another
                               // object, only if you are a MyClass<int> will
                               // this friend apply)
};
GMan
Glee! :D [15char]
GMan
Your code actually doesn't compile in Comeau and I can't find how Standard allows it. I believe `template` before `friend` is necessary due to 14.5.3/3.
Kirill V. Lyadvinsky
I actually had that too :( I removed it because I thought it was unnecessary. (After testing in VS) I should really not test with that... I'll add it back in. Bah!
GMan
I've tested it in VS too, but I can't find such syntax even in C++0x draft N2857. Should be some reason why VS allows it.
Kirill V. Lyadvinsky
@Kiril:: Commonly, the reason is that VC has a bug. `:(`
sbi
+5  A: 

Add MyClass as friend class:

template<typename T> class MyClass
{
    template<typename TX>
    friend class MyClass;
 ...

According to C++ Standard 14.5.3/3:

A friend template may be declared within a class or class template. A friend function template may be defined within a class or class template, but a friend class template may not be defined in a class or class template. In these cases, all specializations of the friend class or friend function template are friends of the class or class template granting friendship. [Example:

class A {
  template<class T> friend class B;  // OK
  template<class T> friend void f(T){ /* ... */ }  // OK
};

—end example]

NOTE: You should know that the code above still could lead to an error with some compilers due to Core Issue #602 which is still open. Despite this, the code above compiles on GCC, Visual C++ and Comeau.

To make only function foo a friend you could write the following:

template<typename T> class MyClass
{
    template<typename TY> template<typename TX> 
    friend void MyClass<TY>::foo(MyClass<TX>);
 ...
Kirill V. Lyadvinsky
So, this code makes MyClass<TX> a friend of MyClass<T> for all TX.Is it possible to make only MyClass<TX>::foo a friend rather than the whole class?
Paul Baker
@Paul, I've updated the answer.
Kirill V. Lyadvinsky