-2

Given this code:

#include <type_traits>
template<char ...Cs>
auto foo() -> typename std::enable_if<(sizeof...(Cs) > 1)>::type{
}

template<char C>
void foo() {
}

int main(){
    foo<'s'>();
}

I have the above c++ program and I'm just wondering, according rules laid out in the standard, which one of the two "foo" templates will be instantiated for the "foo" call in main.

igbgotiz
  • 892
  • 1
  • 8
  • 28
  • 1
    It doesn't print anything ... ? – Fantastic Mr Fox Jun 03 '14 at 02:30
  • 2
    Have you tried running it? Also, there are no obvious `print` functions.... – merlin2011 Jun 03 '14 at 02:30
  • Its been a long day. I meant which foo should be called. – igbgotiz Jun 03 '14 at 02:44
  • None of them. Because you are not calling anything. – juanchopanza Jun 03 '14 at 06:02
  • You should look at SFINAE. First template is disable when `sizeof...(Cs) < 2` as in your case. – Jarod42 Jun 03 '14 at 07:22
  • I'm voting to re-open. The question clearly asks what the STANDARD says, not what someone's compiler says (which may be wrong). And it's a very useful question about variadic templates and how to terminate them properly. – Kaz Dragon Jun 03 '14 at 09:02
  • @KazDragon It's neither. It's a simple demonstration of SFINAE and as the question is unclear whether the result was unexpected or not, it's unclear whether a simple standard-reference to the SFINAE rule would satisfy it. In any case, "how is SFINAE specified" is a very general question which isn't really how this is phrased. – Potatoswatter Jun 08 '14 at 04:45

1 Answers1

0

You can add a print to each one to find out:

template<char ...Cs>
auto foo() -> typename std::enable_if<(sizeof...(Cs) > 1)>::type{
    cout << "1" << endl;
}
template<char C>
void foo() {
    cout << "2" << endl;
}
void EnableIfWithSizeof(){
    foo<'s'>();
}

int main() {
EnableIfWithSizeof();
return 0;
}

The resultant output is 2

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • 1
    Yes I know. Question is which rule in the standard dictates this. – igbgotiz Jun 03 '14 at 02:51
  • Yeah, i am trying to figure that out. – Fantastic Mr Fox Jun 03 '14 at 02:53
  • 1
    This looks similar to the [cppreference example](http://en.cppreference.com/w/cpp/types/enable_if). Since `sizeof...(Cs)>1` is false, `::type` there is a substitution failure, so that line is not considered. – M.M Jun 03 '14 at 22:01