I am playing around with ways to filter types passed to overloaded function templates. I'm using Visual Studio 2013.
Three part question:
- Why cant my compiler deduce
Blorg3? - Is the reason that
TFoo2(argc)generates a compiler error the same as #1? - Is there a way to pass template parameters to a constructor?
Here is the sample code:
#include <type_traits> #define IFPTR(T,R) typename std::enable_if<std::is_pointer<T>::value, R>::type #define IFINT(T,R) typename std::enable_if<std::is_integral<T>::value, R>::type template <class T, IFINT(T, T)* = nullptr> int Blorg1(T n) { return n + 1; } template <class T, IFPTR(T, T)* = nullptr> int Blorg1(T n) { return *n + 1; } template <class T> IFINT(T, int) Blorg2(T n) { return n + 1; } template <class T> IFPTR(T, int) Blorg2(T n) { return *n + 1; } template <class T> int Blorg3(IFINT(T, T) n) { return n + 1; } template <class T> int Blorg3(IFPTR(T, T) n) { return *n + 1; } struct TFoo1 { template <class T, IFINT(T, T)* _ = nullptr> TFoo1(T n) { } }; struct TFoo2 { template <class T> TFoo2(IFINT(T, T) n) { } }; int main(int argc, char* argv[]) { Blorg1(argc); // intellisense not happy Blorg2(argc); Blorg3<int>(argc); // why cant deduce? Blorg1(*argv); // intellisense not happy Blorg2(*argv); Blorg3<char*>(*argv); // why cant deduce? (void)TFoo1(argc); // intellisense not happy (void)TFoo2(argc); // intellisense not happy and !!wont compile!! return 0; }