c++ - C++ 中的特定模板友元

标签 c++ templates friend access-control

我有一个关于 C++ 中特定模板友元的问题。 在C++ Primer一书中,具体的模板友情是这样写的:

 template <class T> class Foo3;
 template <class T> void templ_fcn3(const T&);
 template <class Type> class Bar {
     // each instantiation of Bar grants access to the
     // version of Foo3 or templ_fcn3 instantiated with the same type
     friend class Foo3<Type>;
     friend void templ_fcn3<Type>(const Type&);
     // ...
 };

特别之处在于有

<Type>

friend 语句中的类名或函数名之后。

然而,在实践中,如果我这样写:

template <class Type> class T_CheckPointer;
template <class T> T_CheckPointer<T> operator+(const T_CheckPointer<T> &, const size_t n);

template <typename Type>
class T_CheckPointer {

    // Specific Template Friendship
    friend T_CheckPointer<Type>
    operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);

// other code...

}

模板函数在实例化时会出错。

如果我改变

// Specific Template Friendship
friend T_CheckPointer<Type>
    operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);

// Specific Template Friendship
friend T_CheckPointer<Type>
    operator+ <> (const T_CheckPointer<Type> &, const size_t n);

把函数名后面的type去掉就可以了。

谁能告诉我原因?


求助,调用时有错误信息

int iarr[] = {1, 2, 3, 4};
T_CheckPointer<int> itcp(iarr, iarr+4);

错误信息:

/usr/include/c++/4.4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
/usr/include/c++/4.4/bits/stl_iterator.h:96:   instantiated from ‘std::reverse_iterator<int>’
../Classes/T_CheckPointer.hpp:31:   instantiated from ‘T_CheckPointer<int>’
../PE16.cpp:520:   instantiated from here
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:127: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:128: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘int’ is not a class, struct, or union type

最佳答案

这是一个最小的例子:

template<typename T> struct U { typedef typename T::X X; };
template<typename T> void foo(typename U<T>::X);

template<typename T> struct S;
template<typename T> void foo(S<T>);
template<typename T> struct S { friend void foo<T>(S<T>); };

template struct S<int>;

friend 的原因声明失败的原因是,通过提供模板参数的完整列表,您要求编译器专门化所有可用的函数模板,并选择与签名最匹配的模板。特化头定义foo结果特化 U带有导致程序格式错误的参数。

相反,如果您省略模板参数,它将从参数中推导出来。由于这样的模板参数推导是根据14.8.2 [temp.deduct]执行的,特别是14.8.2p8适用,这意味着U的特化中的替换失败。不是错误 (SFINAE)。

这是在任何可以从上下文中推导出模板参数的地方省略模板参数的一个很好的理由(例如,这里的函数或运算符参数类型)。请注意,您仍然需要提供 <>括号以确保 operator +被读取为 template-id (14.5.4 [temp.friend])。

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

相关文章:

templates - Go 模板 - 从对象存储/数据库加载

c++ - 在专用模板中访问嵌套 friend 类的私有(private)成员

c++ - 友元函数中指向基类参数类型的指针

c++ - C++使用外部嵌套结构实现类函数

C++ DLL 返回指向 std::list<std::wstring> 的指针

bash - 是否可以仅使用命令行从模板创建新的 git 存储库?

C++。编译错误。我正在尝试使用枚举模板参数添加 friend 模板函数

c++ - 如何理解C++ FAQ中的 "program ignoring my input request"?

c++ - 使用 gdb 调试多线程代码但无法访问私有(private)变量?

c++ - 模板中关键字 'typename' 和 'class' 的区别?