c – 使用函数作为模板参数的部分特化拒绝
发布时间:2020-12-16 07:21:22 所属栏目:百科 来源:网络整理
导读:得到了以前版本的 gcc用于编译的代码: template int line,typename FuncSig,FuncSig fstruct HelperWrapper;// [...]template int line,typename Ret,Ret (Func)()struct HelperWrapperline,Ret ()(),Func{ static inline int WrapFuncT(const int) { retur
得到了以前版本的
gcc用于编译的代码:
template <int line,typename FuncSig,FuncSig f> struct HelperWrapper; // [...] template <int line,typename Ret,Ret (&Func)()> struct HelperWrapper<line,Ret (&)(),Func> { static inline int WrapFuncT(const int) { return 0; // Changed } }; // Unary template <int line,typename Arg1,Ret (&Func)(Arg1)> struct HelperWrapper<line,Ret (&)(Arg1),Func> { static inline int WrapFuncT(const int) { return 1; // Changed } }; // Binary template <int line,typename Arg2,Ret (&Func)(Arg1,Arg2)> struct HelperWrapper<line,Ret (&)(Arg1,Arg2),Func> { static inline int WrapFuncT(const int) { return 2; // Changed } }; 被GCC 7.1.1拒绝,但有错误: a.hpp:683:16: error: partial specialization 'struct Type::Implementation::HelperWrapper<line,Func>' is not more specialized than [-fpermissive] struct HelperWrapper<line,Ret (&)(void),Func> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a.hpp:640:16: note: primary template 'template<int line,class FuncSig,FuncSig f> struct Type::Implementation::HelperWrapper' struct HelperWrapper; ^~~~~~~~~~~~~ a.hpp:695:16: error: partial specialization 'struct Type::Implementation::HelperWrapper<line,FuncSig f> struct Type::Implementation::HelperWrapper' struct HelperWrapper; ^~~~~~~~~~~~~ a.hpp:707:16: error: partial specialization 'struct Type::Implementation::HelperWrapper<line,Func> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a.hpp:640:16: note: primary template 'template<int line,FuncSig f> struct Type::Implementation::HelperWrapper' struct HelperWrapper; 我不明白这个消息,因为GCC所说的主要模板是,据我所知,它是一个通用模板结构的前向声明,在代码中的任何地方都不存在. 此代码的思想是捕获传入函数的签名和参数类型. 1)GCC是对的吗? (如果您认为不是,请在当前标准中引用支持您声明的内容) 2)如何修复代码,使其被GCC接受(它被Clang一直接受到VisualStudio 2003).我不能用C 11. 编辑:我终于成功向GCC开发人员报告此问题,这是一个应该在下一版本中修复的错误. 解决方法
它似乎是一个编译器错误,但在
GCC Bugzilla上没有找到任何对这个问题的引用.尽管如此,我在
Compiler Explorer测试你的代码与最近的GCC编译器一样,并且使用函数指针代替函数引用也适用于GCC 7.1.这是
live demo.
如果partial template specialization不比主模板更专业,编译器可能会抱怨.但在这种情况下,GCC是错误的,因为您将FuncSig模板参数专门用作函数引用(Ret(&)()).更奇怪的事实是编译器不会抱怨函数指针. 此外,问题的原因似乎不是FuncSig而是f模板参数.当我从主模板及其专业化中删除f时问题消失了: template <int line,typename FuncSig> struct HelperWrapper; template <int line,typename Ret> struct HelperWrapper<line,Ret (&)()> { static inline int WrapFuncT(const int) { return 0; } }; /* ... */ 查看现场演示here. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |