为什么不能“推导出"明确给出的模板参数? [英] why template parameter which is explicitely given can not be "deduced"

查看:81
本文介绍了为什么不能“推导出"明确给出的模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自那个问题:将枚举值与 SFINAE 结合使用

我尝试实施:

enum Specifier
{
    One,
    Two,
    Three
};

template <Specifier, typename UNUSED=void>
struct Foo 
{
        void Bar(){ std::cout << "Bar default" << std::endl;}
};

template <Specifier s , typename std::enable_if<s == Specifier::Two || s == Specifier::One, int>::type>
struct Foo<s>
{
    void Bar(){ std::cout << "Bar Two" << std::endl; }
};


int main()
{
   Foo< One >().Bar();
   Foo< Two >().Bar();
}

失败:

> main.cpp:130:8: error: template parameters not deducible in partial specialization:
  130 | struct Foo<s>
      |        ^~~~~~
   main.cpp:130:8: note:         '<anonymous>'

如何修复那个超级简单的例子?我喜欢 SFINAE :-)

How to fix that super simple example? I like SFINAE :-)

推荐答案

enable_if 放入 Foo 的模板参数列表:

Put the enable_if in Foo's template argument list:

template <Specifier s>
struct Foo<s, typename std::enable_if<s == Specifier::Two || s == Specifier::One, void>::type>
//                                           same as the default type used before ^^^^

演示.

这篇关于为什么不能“推导出"明确给出的模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆