c – 使用非类型参数包进行不明确的类模板实例化
发布时间:2020-12-16 07:21:39 所属栏目:百科 来源:网络整理
导读:我试图专攻Expr: #include tuple#include type_traits#include iostreamtemplateclass Tp,class List struct Expr { Expr(){std::cout "0" std::endl;};};//specialization #1templateclass Tp,int...istruct ExprTp,std::tuplestd::integral_constantint,i
我试图专攻Expr:
#include <tuple> #include <type_traits> #include <iostream> template<class Tp,class List> struct Expr { Expr(){std::cout << "0"<< std::endl;}; }; //specialization #1 template<class Tp,int...i> struct Expr<Tp,std::tuple<std::integral_constant<int,i>...>> { Expr(){std::cout << "1"<< std::endl;}; }; //specialization #2 template<int...i> struct Expr<double,i>...>> { Expr(){std::cout << "2"<< std::endl;}; }; int main() { typedef std::tuple<std::integral_constant<int,1>> mylist; Expr<double,mylist> test{}; return 0; } 但是,我得到以下编译器错误: [x86-64 gcc 6.3] error: ambiguous template instantiation for 'struct Expr<double,1> > >' [x86-64 gcc 6.3] error: variable 'Expr<double,1> > > test' has initializer but incomplete type 在这里,特别是第一个错误困扰我.我试图弄清楚为什么这是一个模棱两可的实例. 编译器不应该选择专业化#2吗? 如果我避免在std :: integral_constant中包装非类型参数包int … i,它会编译没有任何问题,并选择第二个特化.以下示例有效: #include <tuple> #include <type_traits> #include <iostream> template<class Tp,class...args> struct Expr<Tp,std::tuple<args...>> { Expr(){std::cout << "1"<< std::endl;}; }; //specialization #2 template<class...args> struct Expr<double,std::tuple<args...>> { Expr(){std::cout << "2"<< std::endl;}; }; int main() { typedef std::tuple<std::integral_constant<int,mylist> test{}; return 0; } 解决方法
这不可能是正确的.这是一个gcc bug(我找不到它的bug报告,也许还没有报告呢?).
你是对的,必须选择专业化#2.因为有两个匹配的特化,所以部分排序选择最专业的一个,在你的情况下是#2(作为第一个参数的双重比作为第一个参数的任何类型更专业). 另外,clang编译代码without any problems. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |