I am trying to write a function such that f<T>(args..)
returns the first parameter of type T
.
The following program seems to always select the first specialization thus printing 97
(ASCII code of 'a'
). Though the second one wouldn't require converting char
to int
. Could someone please explain the behavior?
I am new to SFINAE and meta-programming.
#include <iostream>
using namespace std;
template <typename T, typename ...Ts>
T f(T a, Ts... args) {
return a;
}
template <typename R, typename T, typename ...Ts>
R f(typename enable_if<!is_same<R, T>::value, T>::type a, Ts... args) {
return f<R>(args...);
}
int main() {
cout << f<int>('a', 12);
}
f
to do? – Idiolect