samedi 1 avril 2017

Check if a function template is unary

I am trying to check if a function argument passed is unary or not, something like so

template <typename Func>
using EnableIfUnary = std::enable_if_t<std::is_same<
    decltype(std::declval<Func>()(std::declval<const int&>())),
    decltype(std::declval<Func>()(std::declval<const int&>()))>::value>;

template <typename Func, EnableIfUnary<Func>* = nullptr>
void do_something(Func func) { ... }

// and use like so
template <typename Type>
void foo(Type) { ... }

int main() {
    do_something(foo);
    return 0;
}

Is there a better way to check if a function is unary? My current approach doesn't work when the function pass in (foo() in my example) uses the type in a way that would not work with ints.

Aucun commentaire:

Enregistrer un commentaire