0

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

我打算看看如果我真的理解发生了什么,所以我编辑了这样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>          ]
4

1 回答 1

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化做朋友,您可以转发声明这些函数,然后像在您的类中所做的那样对它们进行特定的修改,只需稍作修改。像这样的东西。Tprint_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);

这是其中的一个Example

于 2013-05-15T11:12:54.537 回答