C++ 为什么可以';我不能得到这样一个模板的第一个参数吗?
此代码C++ 为什么可以';我不能得到这样一个模板的第一个参数吗?,c++,templates,template-meta-programming,template-argument-deduction,C++,Templates,Template Meta Programming,Template Argument Deduction,此代码 template<class> struct template_arg; template<class T, template<T> class Template, T Value> struct template_arg<Template<Value> > { static T value() { return Value; } }; template<int> struct const_int { }
template<class>
struct template_arg;
template<class T, template<T> class Template, T Value>
struct template_arg<Template<Value> >
{
static T value() { return Value; }
};
template<int>
struct const_int { };
int main()
{
return template_arg<const_int<0> >::value();
}
模板
结构模板参数;
模板
结构模板参数
{
静态T值(){返回值;}
};
模板
结构常数int{};
int main()
{
返回模板参数::值();
}
导致以下错误:
error: template parameters not deducible in partial specialization:
struct template_arg<Template<Value> >
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
错误:部分专门化中无法推断模板参数:
结构模板参数
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
但是,我不明白为什么编译器不能推断出这个模板参数
这背后的原因是什么?是否有一个好的/通用的解决方法?模板和模板值参数是二等公民。对它们的支持真是糟糕透了,以至于当我想进行严肃的元编程时,我对这两种类型都使用代理类型。在C++17中。模板参数的演绎规则已更改。@liliscent-你说得对。。。我的编译器太旧了,无法很好地支持C++17。哦,哇,我没有意识到这一点@轻率:考虑发布答案吗?@ YAKK:你会如何使用代理类型?我不知道你的意思。