edouarda / brigand

Instant compile time C++ 11 metaprogramming library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

for_each instantiates instances of types in list

chadlayton opened this issue · comments

I'm not sure if there's a simple way around this but it would be nice to be able to use for_each on lists that contain types without a default constructor - or better yet avoid instantiating them at all. Right now I work around the problem by transforming my list of types into a list of pointers and then iterating over that.

brigand::for_each wraps types in brigand::type_, as exhibited in this example:

struct counter
{

    template <typename U>
    void operator()(brigand::type_<U>)
    {
        ++_counter;
    }

    int _counter{0};

};


struct no_default
{

    no_default(int i) : _i(i) {}

    int _i;

};

auto r = brigand::for_each<brigand::list<void, int, no_default>>(counter{});
assert(r._counter == 3);

What is currently preventing you from using types without a default constructor?

Ah! I was doing this:

brigand::for_each<std::tuple<void, int, no_default>>(counter{});

I had been using std::tuple and brigand::list interchangeably until this point but obviously there are some differences that I've failed to understand.