1

Suppose I have an boost::mpl::vector "myvec", defined for example like this:

using myvec = boost::mpl::vector<int, double, double>;

Now I want to define another type, myvecex, that transforms each myvec member into std::tuple with added string. I want to get a type defined like this:

using myvecex = boost::mpl::vector<std::tuple<int, std::string>, 
                                   std::tuple<double, std::string>,
                                   std::tuple<double, std::string> >;

But I don't want to repeat myself and name all the vector members. Instead I want to define some_smart_template template type where I will somehow put the logic of converting each member type into a tuple.

using myvecex2 = some_smart_template<myvec>; 
static_assert(std::is_same<myvecex, myvecex2>::value);

Is it doable in C++ at all?

1 Answer 1

1

Boost.MPL doesn't just give you containers, it gives you algorithms over those containers too. In this case, what you want is transform:

template<
      typename Sequence
    , typename Op
    , typename In = unspecified
    >
struct transform
{
    typedef unspecified type;
};

The semantics are you give it a sequence and what MPL refers to as a Lambda Expression and you get out another sequence. Specifically:

using B = mpl::transform<A,
    std::tuple<mpl::_1, std::string>
    >::type;

Or at least that would work if apply supported variadic class templates like std::tuple. So you'll need to just write an operation that's either a metafunction class:

struct tuple_of_strings {
    template <class T>
    struct apply {
        using type = std::tuple<T, std::string>;
    };
};

using B = mpl::transform<A, tuple_of_strings>::type;

or a metafunction:

template <class T>
struct tuple_of_strings {
    using type = std::tuple<T, std::string>;
};

using B = mpl::transform<A, tuple_of_strings<_1>>::type;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.