C++类成员访问模板的问题

Fir*_*cer 11 c++ templates

我遇到了一个问题,如果我有一个模板类,它反过来有一个模板方法,它接受该类的另一个实例的参数(具有不同的模板参数),它不能访问传递给该类的受保护或私有成员参数,例如:

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);
}
Run Code Online (Sandbox Code Playgroud)

是否有人说MyClass <T>中的方法可以完全访问MyClass <SomeOtherT>?

GMa*_*ckG 11

它们是不同的类型:模板从模板构造类型.

您必须对您的班级朋友进行其他实例化:

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)
};
Run Code Online (Sandbox Code Playgroud)


Kir*_*sky 7

添加MyClass为好友类:

template<typename T> class MyClass
{
    template<typename TX>
    friend class MyClass;
 ...
Run Code Online (Sandbox Code Playgroud)

根据C++标准14.5.3/3:

可以在类或类模板中声明朋友模板.可以在类或类模板中定义朋友函数模板,但是可以不在类或类模板中定义朋友类模板.在这些情况下,朋友类或朋友功能模板的所有特化都是授予友谊的类或类模板的朋友.[例:

class A {
  template<class T> friend class B;  // OK
  template<class T> friend void f(T){ /* ... */ }  // OK
};
Run Code Online (Sandbox Code Playgroud)

- 末端的例子]

注意:您应该知道上面的代码仍然可能导致某些编译器出错,因为Core Issue#602仍处于打开状态.尽管如此,上面的代码编译在GCC,Visual C++和Comeau上.

要使只foo为朋友发挥作用,您可以编写以下内容:

template<typename T> class MyClass
{
    template<typename TY> template<typename TX> 
    friend void MyClass<TY>::foo(MyClass<TX>);
 ...
Run Code Online (Sandbox Code Playgroud)