为什么SFINAE仅适用于这两个看似相同的功能之一?

时间:2019-03-01 22:15:43

标签: c++ templates sfinae

以下代码无法编译(对于clang 7.0,--std = c ++ 17):

struct Foo {
    void foo() {}
};
struct Bar {
    void bar() {}
};
struct Both {
    void foo() {}
    void bar() {}
};

template <typename T, typename>
void DoStuff(T input) {
    input.foo();
}

template <typename T, typename...>
void DoStuff(T input) {
    input.bar();
}

int main(int argc, char** argv) {
    DoStuff(Foo());
    return 0;
}

错误是:

<source>:19:11: error: no member named 'bar' in 'Foo'
    input.bar();
    ~~~~~ ^

<source>:23:5: note: in instantiation of function template specialization 'DoStuff<Foo>' requested here
    DoStuff(Foo());

但是,如果将Foo()更改为Bar()(或Both()),则可以正常编译。在Bar()情况下,这表明SFINAE正在生效;在Both()情况下,它表明typename...重载的优先级较低,因此在两个重载都适用时选择另一个重载。

但是我不明白为什么SFINAE适用于Bar()情况而不适用于Foo()情况?

1 个答案:

答案 0 :(得分:2)

这里没有SFINAE。对DoStuff的所有呼叫都将呼叫

template <typename T, typename...>
void DoStuff(T input) {
    input.bar();
}

原因是

template <typename T, typename>
void DoStuff(T input) {
    input.foo();
}

同时需要两个模板参数

template <typename T, typename...>
void DoStuff(T input) {
    input.bar();
}

适用于1个或多个模板参数(可变包允许为空)。所以当你打电话

DoStuff(Foo());
// or
DoStuff(Bar());
//or
DoStuff(Both());

您只能推导单个模板参数,唯一可行的候选对象是

template <typename T, typename...>
void DoStuff(T input) {
    input.bar();
}

如果您曾经使用过

DoStuff<Foo, any_other_type>(Foo());

然后您会得到一个歧义错误,因为它与两个模板都匹配。

相关问题