Are common function wrappers possible in C ++?

This happened when I was looking for an error in a function shell boost::fusion::fusedwhen using decltype. The problem is that the invalid decltype type is a compilation error, even if the required template instance is not used, and I cannot figure out how to get around this in order to create a common wrapper of functions.

Here is my one-argument wrapper attempt:

#include <utility>
#include <type_traits>

template <class T>
typename std::add_rvalue_reference<T>::type declval();

template <class Fn, class Arg>
struct get_return_type
{
    typedef decltype(declval<Fn>()(declval<Arg>())) type;
};

template <class Fn>
struct wrapper
{
    explicit wrapper(Fn fn) : fn(fn) {}
    Fn fn;

    template <class Arg>
    typename get_return_type<Fn,Arg&&>::type
        operator()(Arg&& arg)
    {
        return fn(std::forward<Arg>(arg));
    }

    template <class Arg>
    typename get_return_type<const Fn,Arg&&>::type
        operator()(Arg&& arg)
    {
        return fn(std::forward<Arg>(arg));
    }
};

The problem is that this does not work for cases where the arguments of the non-envelope version are not converted to arguments for the const version. For instance:

#include <iostream>

struct x {};
struct y {};

struct foo
{
    void operator()(x) { std::cout << "void operator()(x)" << std::endl; }
    void operator()(y) const { std::cout << "void operator()(y) const" << std::endl; }
};

int main()
{
    wrapper<foo> b = wrapper<foo>(foo());
    b(x()); // fail
}

It seems to me that the failure of the decltype expression caused by void operator()(y) constit should simply lead to the removal of this function due to SFINAE.

+3
source share
1

, g++ 4.6.1:

#include <utility>
#include <type_traits>
#include <iostream>

template <class Fn, class Arg>
struct get_return_type
{
    typedef decltype( std::declval<Fn>() ( std::declval<Arg>() ) ) type;
};

template <class Fn>
struct wrapper
{
    explicit wrapper(Fn fn) : fn(fn) {}
    Fn fn;

    template <class Arg>
    typename get_return_type<Fn,Arg&&>::type
    operator()(Arg&& arg)
    {
        return fn(std::forward<Arg>(arg));
    }

    template <class Arg>
    typename get_return_type< const Fn,Arg&&>::type
    operator()(Arg&& arg) const
    {
        return fn(std::forward<Arg>(arg));
    }
};

struct x {};
struct y {};

struct foo
{
    x* operator()(x) { std::cout << "x* operator()(x)" << std::endl; return nullptr; }
    x operator()(x) const { std::cout << "x operator()(x) const" << std::endl; return x(); }
    y* operator()(y) { std::cout << "y* operator()(y)" << std::endl; return nullptr; }
    y operator()(y) const { std::cout << "y operator()(y) const" << std::endl; return y(); }
};

template <class Fn>
void test_foo(Fn fn)
{
    // make sure all operator() overloads are callable
    const Fn& cfn = fn;

    x* a = fn(x());
    x b = cfn(x());
    y* c = fn(y());
    y d = cfn(y());
(void)a;(void)b;(void)c;(void)d;
}

int main()
{
    test_foo(foo());
    test_foo(wrapper<foo>(foo())); // fail
}
0

All Articles