好友模板功能无法访问私有成员

时间:2013-05-15 10:33:51

标签: c++ templates

我对@GManNickG编写的this代码有疑问。

我会看到如果我真的明白发生了什么,所以我编辑了print_binary_helper这样的朋友功能(原始代码已被评论):

//template <typename U>
//friend print_binary_helper<U> print_binary(U value);
friend print_binary_helper<T> print_binary(T value);

//template <typename U>
//friend std::ostream& operator<<(std::ostream& sink,
//  const print_binary_helper<U> source);
friend std::ostream& operator<<(std::ostream& sink,
    const print_binary_helper<T> source);

//template <typename U>
//friend std::wostream& operator<<(std::wostream& sink,
//  const print_binary_helper<U> source);
friend std::wostream& operator<<(std::wostream& sink,
    const print_binary_helper<T> source);

使用T代替U但程序不会编译。有人可以向我解释我做错了什么,如果这是可能的,如果是的话,怎么可能呢?

我正在使用VC ++ 11,这是我得到的错误:

1>anything.cpp(68): error C2248: 'print_binary_helper<T>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<T>'
1>          with
1>          [
1>              T=int
1>          ]
1>          anything.cpp(31) : see declaration of 'print_binary_helper<T>::print_binary_helper'
1>          with
1>          [
1>              T=int
1>          ]
1>          anything.cpp(73) : see reference to function template instantiation 'print_binary_helper<T> print_binary<int>(T)' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>anything.cpp(68): error C2248: 'print_binary_helper<T>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<T>'
1>          with
1>          [
1>              T=unsigned __int64
1>          ]
1>          anything.cpp(31) : see declaration of 'print_binary_helper<T>::print_binary_helper'
1>          with
1>          [
1>              T=unsigned __int64
1>          ]
1>          anything.cpp(75) : see reference to function template instantiation 'print_binary_helper<T> print_binary<unsigned __int64>(T)' being compiled
1>          with
1>          [
1>              T=unsigned __int64
1>          ]

1 个答案:

答案 0 :(得分:1)

template <typename U>
friend print_binary_helper<U> print_binary(U value);

成为模板 print_binary功能好友。

friend print_binary_helper<U> print_binary(U value);

制作非模板 print_binary功能好友。

两者不同。因此,在您的情况下,模板功能不是朋友非模板功能未定义。你没有得到任何错误,因为你没有在任何地方使用非模板print_binary

这些功能是朋友。所以他们不应该依赖于类的模板参数。它们应该是独立的功能。


如果您只想对T类的T专业版进行print_binary_helper专业化的template <typename T> class print_binary_helper; template <typename T> std::ostream& operator<<(std::ostream& sink, const print_binary_helper<T> source); template <typename T> std::wostream& operator<<(std::wostream& sink, const print_binary_helper<T> source); template <typename T> print_binary_helper<T> print_binary(T value); template <typename T> class print_binary_helper { public: static_assert(std::is_integral<T>::value, "Cannot print non-integer in binary."); //make only print_binary<T> a friend to print_binary_helper<T> friend print_binary_helper<T> print_binary<>(const T value); // ^^ friend std::ostream& operator<< <>(std::ostream& sink, // ^^ const print_binary_helper<T> source); friend std::wostream& operator<< <>(std::wostream& sink, // ^^ const print_binary_helper<T> source); 专业化,您可以转发声明函数,然后像在课堂上一样对它们进行特化处理稍作修改。有点像这样。

{{1}}

以下是Example