mardi 23 décembre 2014

I need to accomplish the following:



template<typename T>
f() {
:
return { -1 if T is of integral type, else nullptr }
}


In my particular use case, T could be one of four types:



int
Py_ssize_t // ssize_t
Py_hash_t // ssize_t

PyObject* // PyObject is some C struct


This is the best solution I have so far:



template<typename T>
T test(typename enable_if<is_integral<T>::value, void*>::type = nullptr)
{ return -1; }

template<typename T>
T test(typename enable_if<is_pointer<T>::value, void*>::type = nullptr)
{ return nullptr; }


ideone


But is this using a sledgehammer to crack a nut?


My only objection is that it solves a wider problem scope, but I'm not sure whether it is complete without additional fiddling with std::decay.


I make a habit of resisting the temptation to complexify code so that it can also solve imaginary extensions of the problem scope at hand.


But in this case I can't see a simpler solution.


Aucun commentaire:

Enregistrer un commentaire