S
S
sstepashka2013-11-05 07:18:53
Scala
sstepashka, 2013-11-05 07:18:53

Dependent method type (C++)

If an example of the implementation of such a thing as Dependent method type on Scala. ( link ). Is it possible to do this in C++11 (for example) or in some other language with static typing? The whole point here is to use a functional approach, but with a “hack” in the form of a class.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2013-11-05
@AxisPod

You need an overload on the function's input parameter. It's just that the check of the if / else condition should be shifted to the shoulders of overloaded methods, it is impossible to compare universally inside the method. Make a helper template function.

template<typename T>
struct type2type { typedef T type; }

temlate<typename U, typename T>
U helper_func(T const& method, type2type<GetX>) { return [](){return 1;}; }

temlate<typename U, typename T>
U helper_func(T const& method, type2type<ToString>) { return [](){return "aaaa";}; }

// и вызов
template <typename T>
std::result_of<typename T::signature> func(T const& method) {
    return helper_func<std::result_of<typename T::signature>, T>(method, type2type<T>());
}

T
Teivaz, 2013-11-06
@Teivaz

There is an option. In the sense of boost:: option.
boost::variant

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question