dimanche 26 juillet 2015

c++11 std::function like functors incomplete type

I've stumbled upon something I can't get through since last week...

Having this:

template<typename> struct fx;

template<typename R, typename...Args>
struct fx<R(Args...)>
{
    virtual R operator()(const Args & ...x) const = 0;
};

and this:

template<typename> struct fx_err;

// I feel here something is wrong, but I can't figure it out.
template<template<typename> class F, typename R, typename... Args>
struct fx_err< F<R(Args...)> > : fx<R(R,Args...)>
{
    using fx_type = F<R(Args...)>;

    fx_type f;

    R operator ()(const R &y, const Args & ...x) const override
    {
        return y - f(x...);
    }
};

and this:

struct example_fun : fx<int(int,int,int)>
{
    int operator() (const int &a, const int &b, const int &c) const override
    {
        return a * b * c;
    }
};

when finally I try to use it like:

fx_err<example_fun> example;
int err = example(24,2,3,4);

compiler throws error: 'example has incomplete type'.

Something similar works only if I do not specialize fx_err and use pointer to fx_type functor instead, but then I need to add constructor to grab the pointer itself which is not something I want.

It's very frustrating. What's wrong with this? Is it possible what I'm trying to achieve? Can anybody help?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire