Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use brace-enclosed initializer lists in a variadic template?

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>)’
like image 259
djfm Avatar asked Aug 12 '12 10:08

djfm


1 Answers

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

like image 68
Johannes Schaub - litb Avatar answered Nov 02 '22 09:11

Johannes Schaub - litb