Alex Rivera | Logout

Use brace-enclosed initializer lists in a variadic template?

Asked 2012-08-12T10:02:09.200
8

I'm trying to use brace-enclosed initializer lists in a variadic template function, but the compiler complains... am I asking too much or did I do something wrong?

This is best demonstrated by example:

struct Bracy
{
    Bracy(int i, int j)
    {
    }
};

struct Test
{

    void consumeOne(int i)
    {
    }

    void consumeOne(const Bracy & bracy)
    {
    }

    void consume()
    {
    }

    template<typename T, typename ...Values>
    void consume(const T & first, Values... rest)
    {
        consumeOne(first);
        consume(rest...);
    }

    template<typename ...Values>
    Test(Values... values)
    {
        consume(values...);
    }
};

void testVariadics()
{
    Test(7,{1,2}); //I'd like {1,2} to be passed to consumeOne(const Bracy & bracy)
}

GCC (4.7) says:

main.cpp:45:14: error: no matching function for call to ‘Test::Test(int, <brace-enclosed initializer list>)’
Edit
Report

1 Answer

2

A brace enclosed initializer list cannot be forwarded, so you are unfortunately out of luck.

answered 2012-08-12T12:30:54.100

Your Answer