mardi 3 octobre 2017

C++11 How to select a specialization by non-type parameter

I would like to define a variadic template class with non-type parameters and use a nested template class for which I could to get a specialization by one of variadic template parameters.

My question is similar to C++ variadic template with non-type parameters of different types. But I would like to make a specialization for a nested class one of the parameters of the owner class

struct Data
{
    std::string field1;
    std::string field2;
    bool field3 = false;
    int field4 = 0;
};

template <typename ... Types>
struct Wrapper
{
    template <Types ... Args>
    struct Holder
    {
    };
};

It's valid:

using W = Wrapper<decltype(&Data::field1), decltype(&Data::field2), decltype(&Data::field3), decltype(&Data::field4)>;
    using H = W::Holder<&Data::field1, nullptr, nullptr, nullptr>;

But I need to use this as

using H = W::Holder<&Data::field3>;

How can I implement this?

The similar code

#include <iostream>
#include <string>
#include <type_traits>

struct Data
{
    std::string field1;
    std::string field2;
    bool field3 = false;
    int field4 = 0;
};

struct Null;

template <typename T, T>
struct Holder
{
};

template <typename H, typename ... T>
struct Select
    : Select <T ... >
{
    using Base = Select <T ... >;
    using Base::Get;

    template <H F>
    static Holder<H, F> Get();
};

template <>
struct Select<Null>
{
    static void Get();
};

int main()
{
    using S = Select<std::string Data::*, bool Data::*, int Data::*, Null>;
    using H = decltype(S::Get<&Data::field3>());
    static_assert(std::is_same<H, Holder<decltype(&Data::field3), &Data::field3>>::value, "Problem ...");
    return 0;
};

But I need do this without decltype and functions

The right solution will be

using H = W::Holder<&Data::field3>;

Aucun commentaire:

Enregistrer un commentaire