为什么不能“推导出"明确给出的模板参数?
问题描述:
来自那个问题:将枚举值与 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 ^^^^
演示.