4

在 Linux 上使用 GCC 4.8.2,我想授予工厂方法 Create() 对 C 类的私有构造函数的访问权限,但是在尝试声明专门的朋友时出现“错误:'Create' 未在此范围内声明” . 如何在不对 B::Create() 的所有类型开放声明的情况下使其工作?

template <typename T> class A {
 public:
  class B;
  template <typename U> class C;
};

template <typename T>
class A<T>::B {
 public:
  template <typename U> static void Create();
};

template <typename T> template <typename U>
class A<T>::C {
  C() = default;
  friend void B::Create<U>();
};

template <typename T> template <typename U>
void A<T>::B::Create() {
  C<U>{};
}

int main() {
  A<int>::B::Create<char>();
}
4

2 回答 2

1

我认为您遇到了编译器缺陷。以下代码仅使用一层模板,在 g++ 4.8.2 中运行良好。

class Foo
{
   public:
      template <typename U> static Foo* Create();
};

template <typename U> class Bar : public Foo
{
   private:
      Bar() = default;

      friend Foo* Foo::Create<U>();
};

template <typename U>
Foo* Foo::Create() {
  return new Bar<U>();
}

但是,相同的编译器无法编译您的代码。我认为您已经尝试过的一种解决方法是替换

friend B* B::Create<U>();

template <typename V> 
friend B* B::Create<V>();
于 2015-02-21T08:32:28.317 回答
1

我刚刚在 MSVC++ 中测试了你的代码,它也在那里失败了。我可以通过给朋友声明一个更明确的名称来解决它。请记住,您可以在任何地方提供完全限定的名称!

我刚换

friend B* B::Create<U>();

friend B* A<T>::B::Create<U>();

我会启动 MinGW,看看它是否真的按照您的预期做。

于 2015-02-21T10:57:38.120 回答