sp2danny
sp2danny

Reputation: 7644

Why do I not need SFINAE here

I was trying to do a motivating example for SFINAE, when I noticed that you don't need that for the following:

#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);
}

This compiles, runs, and prints:

beg, end
count, val

try it here

I do not understand why the second call to add is not ambiguous.

Upvotes: 3

Views: 129

Answers (1)

Jarod42
Jarod42

Reputation: 217135

Basically:

  • Both overloads are viable.
  • Both are match without conversion/promotion.
  • add(int count, const T& val) is preferred as non template.

Upvotes: 10

Related Questions