C中的具体模板友谊

C中的具体模板友谊,第1张

概述我对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 acc 我对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 FrIEndshipfrIEnd T_CheckPointer<Type>    operator+ <Type> (const T_CheckPointer<Type> &,const size_t n);

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

通过删除功能名称后面的单词类型,那么一切都会好的.

谁能告诉我原因?

有关信息,我打电话时会出现错误消息

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,or union type/usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘int’ is not a class,or union type/usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘int’ is not a class,or union type/usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘int’ is not a class,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).

这是一个很好的理由,可以在任何可以从上下文推导出的模板参数中省略(例如这里的函数或运算符参数类型).请注意,您仍需要提供<>括号,以确保 *** 作符被读作模板ID(14.5.4 [temp.frIEnd]).

总结

以上是内存溢出为你收集整理的C中的具体模板友谊全部内容,希望文章能够帮你解决C中的具体模板友谊所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1238295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存