为什么我在这里不需要Sfinae

Why do I not need SFINAE here

本文关键字:Sfinae 不需要 在这里 为什么      更新时间:2023-10-16

我试图为Sfinae做一个激励榜样我注意到您不需要以下内容:

#include <iostream>
using namespace std;
template<typename T>
struct Foo
{
    void add(int count, const T& val) // add "count" number of val's
    {
        cout << "count, val" << endl;
    }
    template<typename It>
    void add(It beg, It end) // add items [beg,end)
    {
        cout << "beg, end" << endl;
    }
};
int main()
{
    int a=1;
    int xx[] = {1,2,3};
    Foo<int> foo;
    foo.add(xx, xx+3);
    foo.add(2, a);
}

此编译,运行和打印:

乞讨,结束
计数,val

在这里尝试

我不明白为什么第二个呼叫add不模棱两可。

基本上:

  • 两个过载都是可行的。
  • 两者都是匹配的,没有转换/促销。
  • add(int count, const T& val)是非模板。