mardi 1 mars 2016

Trailing return type usage when using CRTP

The following is a mockup code that I wrote to experiment with trailing return types in a CRTP setup.

#include <iostream>
#include <memory>
#include <utility>

using namespace std;

struct t_aspect{
    struct t_param1 {};
};

// Generic Selector
template <typename t_detail>
struct Select;

template <>
struct Select<t_aspect::t_param1> {
    using typeof = t_aspect::t_param1;
};

//Base CRTP class
template<typename dclas>
class CrtpB
{
    public:
        template<typename T1>
        auto func1() -> // What should be here?
        {
            return(static_cast<dclas*>(this)->func1<T1>());
        }
};


//Derived CRTP class
class CrtpD : public CrtpB<CrtpD>
{
    private:
        uint32_t param1 = 0;

    private:
        auto func1(const t_aspect::t_param1&) -> uint32_t
        {
            return(param1);
        }

    public:
        static auto create() -> unique_ptr<CrtpB>
        {
            return(unique_ptr<CrtpD>(new CrtpD));
        }

        template<typename T1>
        auto func1() -> decltype(func1(typename Select<T1>::typeof()))
        {
            return(func1(typename Select<T1>::typeof()));
        }
};


int main()
{
    auto crtp = CrtpD::create();
    auto parm = crtp->func1<t_aspect::t_param1>();
    return 0;
}

I would like some help in deciphering what should be the trailing return type of func1 in CrtpB.

I have tried using

decltype(static_cast<dclas*>(this)->func1<T1>())

but this does not work. I have also tried writing a helper function based on a solution found in Inferring return type of templated member functions in CRTP.

template <typename D, typename T>
struct Helpr {
    typedef decltype(static_cast<D*>(0)->func1<T>()) type;
};

But this does not work either.

Aucun commentaire:

Enregistrer un commentaire