c++ - 依赖于模板类的模板友元

标签 c++ templates friend

通过阅读标准试图找出这种语法的几个小时后,我放弃了尝试理解它。 我有一个带有模板构造函数的模板类,我需要部分专门化,因为这是不可能的,我将构造函数实现包装在不同的模板中:

template<class T> 
struct A; //forward declaration
namespace impl {
template<class T>
struct wrap {
    template<class U>
    struct AConstructor {
        static void construct(A<T>*thisPtr) {...}
    };
    //some partial specialization...
};
template<class T>
struct dummy{};
} //end of namespace
template<class T>
struct A {
   template<class U>
    A(dummy<U>) {
        impl::template wrap<T>::template AConstructor<U>::construct(this);
    }
};

这本来可以正常工作,但我需要访问 A<T> 的私有(private)成员在构造函数实现中,所以我需要一个友元声明,这就是它变得棘手的地方。 我尝试使用或不使用 template在可能的位置出现编译错误的关键字是 visual studio:第一个错误 C2992(无效或缺少模板参数列表),其他错误 C2952(模板声明缺少模板参数列表):

template<class T>
class A {
    //1
    template<class U>
    friend struct impl::template wrap<T>::template AConstructor;
    //2,3 I also tried removing the static identifier with this declaration
    template<class U>
    friend void impl::template wrap<T>::template AConstructor<U>::construct(A<T>*);

我怎样才能成为这个函数/结构的 friend ?

最佳答案

[编辑: 以下适用于我机器上的 g++ 4.9.0 和 clang 3.5:

template<class T>
struct dummy {};

template<class T>
struct A {
private:
  template<class U>
  struct AConstructor {
    static void construct(A<T>* thisPtr) {
      thisPtr->foo = 42;
    }
  };

  template<class>
  friend class AConstructor;

public:
  template<class U>
  A(dummy<U>) {
    AConstructor<U>::construct(this);
  }

  void print() const {
    std::cout << foo << std::endl;
  }

private:
  int foo;
};

int main() {
  A<void> foobar( (impl::dummy<void>()) );
  foobar.print();
  return 0;
}

]

对我来说,这是:

template<class T> 
struct A; //forward declaration

namespace impl {

  template<class T>
  struct wrap {
    template<class U>
    struct AConstructor {
      static void construct(A<T>* thisPtr) {
        thisPtr->foo = 42;
      }
    };
    //some partial specialization...
  };

  template<class T>
  struct dummy{};

} //end of namespace

template<class T>
struct A {
  template<class>
  friend class impl::wrap<T>::AConstructor;

  template<class U>
  A(impl::dummy<U>) {
    impl::template wrap<T>::template AConstructor<U>::construct(this);
  }

  void print() const {
    std::cout << foo << std::endl;
  }

private:
  int foo;
};

int main() {
  A<void> foobar( (impl::dummy<void>()) );
  foobar.print();
  return 0;
}

使用 g++ 4.9.0 编译并按预期工作(它在 stdout 上显示 42)尽管编译失败并出现 clang 3.5,我想说这是一个 clang 错误,但我必须承认我不太确定。 不过,如果是这样的话,您会发现它很有用:
Class template with template class friend, what's really going on here?
Template friend

但是,我不太喜欢那种设计,你确定你真的需要构造函数的部分特化吗?您不能用 SFINAE 丢弃它们,或访问具有特征类的某些子类型(或您想要部分特化的任何其他东西,可以用其他东西代替)?
例如这些结构之一:

#include <list>
#include <vector>

template<class T> 
struct is_vector : std::false_type {};

template<class T>
struct is_vector<std::vector<T>> : std::true_type {};

struct A {
  template<
    class T, typename std::enable_if<is_vector<T>::value, void*>::type = nullptr
  >
  A(T) {
    std::cout << "construction with a vector" << std::endl;
  }

  template<class T>
  A(std::list<T>) {
    std::cout << "construction with a list of " << typeid(T).name();
    std::cout << std::endl;
  }

  template<
    class T,
    typename std::enable_if<!is_vector<T>::value, void**>::type = nullptr
  >
  A(T) {
    std::cout << "construction with an iterator (supposedly) whose value_type "
                 "is";
    std::cout << typeid(typename std::iterator_traits<T>::value_type).name();
    std::cout << std::endl;
  }
};

int main() {
  A(std::vector<int>());
  A(std::list<std::vector<int>>());
  A((char*) nullptr);
  return 0;
}

在我的机器上显示这个:

construction with a vector
construction with a list of NSt3__16vectorIiNS_9allocatorIiEEEE
construction with an iterator (supposedly) whose value_type isc

(当然我不知道你到底想做什么,所以我不确定,我会对此发表评论而不是把它放在答案中,但我没有足够的声誉来做所以,今天创建的帐户)

关于c++ - 依赖于模板类的模板友元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712532/

相关文章:

c++ - C、C++ 中 __func__ 标识符的用例?

c++ - 如何使类定义之外的运算符重载优先?

c++ - 不明确的调用 - 模板化函数

c++ - 与其他模板类中的所有模板类成为 friend

c++ - 如何定义全局可见运算符

c++ - 类、构造函数和指针类成员

c++ - 遗留 C/C++ 项目中的死代码检测

c++ - 如何在不知道C++中有多少个可选参数的情况下在循环中使用va_arg?

c++ - 错误 C2823 : a typedef template is illegal - function pointer

c++ - 使类模板成为其自身的 friend 以进行不同的实例化