C # style Action <T>, Func <T, T> etc. In C ++ 0x

C # has common types of functions, such as Action<T>orFunc<T,U,V,...>

With the advent of C ++ 0x and the ability to have typedef template parameters and a variadic template, it seems like this should be possible.

The obvious solution for me would be the following:

template <typename T>
using Action<T> = void (*)(T);

however, this is not suitable for functors or C ++ 0x lambdas, and in addition, it does not compile with the error " expected unqualified-id before 'using'"

My next attempt was to possibly use boost :: function:

template <typename T>
using Action<T> = boost::function<void (T)>;

This does not compile for the same reason.

My only idea is the arguments of the STL style template:

template <typename T, typename Action>
void foo(T value, Action f) {
    f(value);
}

But this does not provide a strictly typed solution and only makes sense in a boilerplate function.

, , ++ wiz, , , , , .

++ #?

+3
3

, :

template <typename T>
using Action = void (*)(T);

(g++ v4.3.2).

STL , , , , . , , Action T. , , Action - , operator().

, ++ 0x typedef, typedef:

template <typename T>
struct Action {
   typedef boost::function<void (T)> type;
};

template <typename T>
void foo(T value, typename Action<T>::type f) {
    f(value);
}
+6

Gcc (. ), , . , .

std:: function std:: function Action Func.

, Action Func std:: function, .

+1

, ++, ++ - .

++ - - , () , . , operator() .. , ++ , , , .

C # generators do not work like this - they are more restrictive, probably in an attempt to improve error messages (since getting C ++ templates is mistakenly cool in output). I don’t know too much about C # generic tools, but I think they rely on something that looks more like callbacks, hence the utility of generic functions.

+1
source

Source: https://habr.com/ru/post/1742217/


All Articles