c – 模板类中的模板化函数

c – 模板类中的模板化函数,第1张

概述参见英文答案 > Where and why do I have to put the “template” and “typename” keywords?                                    6个 同 template <typename T>class Foo {public: template <int x> void bar () { 参见英文答案 > Where and why do I have to put the “template” and “typename” keywords?6个
template <type@R_301_6889@ T>class Foo {public:    template <int x>    voID bar () {}};

以下编译:

voID foobar (){    Foo<int> f;    f.bar<1>();}

但是以下内容没有(带有“错误:在’之前预期的primary-Expression’)’令牌”在gcc 5.4.0中,-std = c 14).

template <type@R_301_6889@ T>voID foobar (){    Foo<T> f;    f.bar<1>();}

如果我尝试显式调用第二个版本,例如

foobar<int>();

然后gcc另外抱怨

"invalID operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'".

有没有理由说第二个版本无效?为什么gcc处理’<'作为运算符而不是模板参数列表的开头?

解决方法 利用模板化函数,编译器不确切地知道Foo< T>.将(可能有Foo的特化),所以它必须假设f.bar是一个成员变量并解析代码为
f.bar < 1

然后它无法继续.

您可以通过告诉编译器栏是模板来帮助编译器

f.template bar<1>();
总结

以上是内存溢出为你收集整理的c – 模板类中的模板化函数全部内容,希望文章能够帮你解决c – 模板类中的模板化函数所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/langs/1242427.html

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

发表评论

登录后才能评论

评论列表(0条)

保存