4
我试图专注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,std::tuple<std::integral_constant<int,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, std::tuple<std::integral_constant<int, 1> > >'
[x86-64 gcc 6.3] error: variable 'Expr<double, std::tuple<std::integral_constant<int, 1> > > test' has initializer but incomplete type
这里,尤其是第一个错误困扰我。我试图弄清楚为什么这是一个模棱两可的实例。
编译器应该选择specialization #2
吗?
如果我避免将非类型参数包int...i
包装在std::integral_constant
中,它会毫无问题地进行编译,并选择第二种特殊化。下面的示例工程:
#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, 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,1>> mylist;
Expr<double,mylist> test{};
return 0;
}