相关疑难解决方法(0)

在C++中,为什么不能使用另一个类的模板类型来管理模板类成员函数?

换句话说,为什么编译正常:

template<typename Type>
class A{
  public:
    void f();
};

class B{
  friend void A<int>::f();
};

template<>
void A<int>::f(){
  B* var = new B();
}
Run Code Online (Sandbox Code Playgroud)

虽然这不是:

template<typename Type>
class A{
  public:
    void f();
};

template<typename Type> // B is now a templated class
class B{
  friend void A<Type>::f(); // Friending is done using B templated type
};

template<>
void A<int>::f(){
  B<int>* var = new B<int>(); // var is now declared using int as its templated type
}
Run Code Online (Sandbox Code Playgroud)

对于第二个代码片段,编译器(gcc 6.2,没有特殊标志)说:

main.cpp: In …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend

25
推荐指数
1
解决办法
398
查看次数

标签 统计

c++ ×1

friend ×1

templates ×1