C++ 朋友模板函数可以';无法访问私人成员

C++ 朋友模板函数可以';无法访问私人成员,c++,templates,C++,Templates,我有一个关于@GManNickG编写的代码的问题 我想看看我是否真的了解发生了什么,所以我编辑了print\u binary\u helper的好友函数,如下所示(原始代码已被注释): //模板 //朋友打印\U二进制\U助手打印\U二进制(U值); 朋友打印\u二进制\u助手打印\u二进制(T值); //模板 //friend std::ostream&运算符T=int 1> ] 1> cpp(31):参见“print\u binary\u helper::print\u b

我有一个关于@GManNickG编写的代码的问题

我想看看我是否真的了解发生了什么,所以我编辑了
print\u binary\u helper
的好友函数,如下所示(原始代码已被注释):

//模板
//朋友打印\U二进制\U助手打印\U二进制(U值);
朋友打印\u二进制\u助手打印\u二进制(T值);
//模板
//friend std::ostream&运算符T=int
1>          ]
1> cpp(31):参见“print\u binary\u helper::print\u binary\u helper”的声明
1> 与
1>          [
1> T=int
1>          ]
1> cpp(73):请参阅正在编译的函数模板实例化“print\u binary\u helper print\u binary(T)”的参考
1> 与
1>          [
1> T=int
1>          ]
1> anything.cpp(68):错误C2248:“print\u binary\u helper::print\u binary\u helper”:无法访问在类“print\u binary\u helper”中声明的私有成员
1> 与
1>          [
1> T=无符号uuu int64
1>          ]
1> cpp(31):参见“print\u binary\u helper::print\u binary\u helper”的声明
1> 与
1>          [
1> T=无符号uuu int64
1>          ]
1> cpp(75):请参阅正在编译的函数模板实例化“print\u binary\u helper print\u binary(T)”的参考
1> 与
1>          [
1> T=无符号uuu int64
1>          ]
生成一个非模板
打印二进制
函数

两者是不同的。因此,在您的例子中,模板函数是不是朋友,而非模板函数没有定义。您不会得到任何错误,因为您没有在任何地方使用非模板
print\u binary

功能是朋友。因此,它们不应该依赖于类的模板参数。它们应该是独立的职能


如果您只想使这些函数的
T
专门化成为
T
类的
print\u binary\u 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);
模板
类打印\二进制\辅助程序;
模板

std::ostream&operator在g++4.7.2坦克上运行良好!这有助于我完成一个将友元函数与模板类一起使用的示例。我想我的书已经过时了。
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>          ]
template <typename U>
friend print_binary_helper<U> print_binary(U value);
friend print_binary_helper<U> print_binary(U value);
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);