views:

567

answers:

3

While doing variadic template programming in C++0x on GCC, once in a while I get an error that says "Sorry, unimplemented: cannot expand 'Identifier...' into a fixed-length arugment list." If I remove the "..." in the code then I get a different error: "error: parameter packs not expanded with '...'".

So if I have the "..." in, GCC calls that an error, and if I take the "..." out, GCC calls that an error too.

The only way I have been able to deal with this is to completely rewrite the template metaprogram from scratch using a different approach, and (with luck) I eventually come up with code that doesn't cause the error. But I would really like to know what I was doing wrong. Despite Googling for it and despite much experimentation, I can't pin down what it is that I'm doing differently between variadic template code that does produce this error, and code that does not have the error.

The wording of the error message seems to imply that the code should work according the C++0x standard, but that GCC doesn't support it yet. Or perhaps it is a compiler bug?

Here's some code that produces the error. Note: I don't need you to write a correct implementation for me, but rather just to point out what is about my code that is causing this specific error

// Used as a container for a set of types.
template <typename... Types> struct TypePack
{
    // Given a TypePack<T1, T2, T3> and T=T4, returns TypePack<T1, T2, T3, T4>
    template <typename T>
    struct Add
    {
        typedef TypePack<Types..., T> type;
    };
};

// Takes the set (First, Others...) and, while N > 0, adds (First) to TPack.
// TPack is a TypePack containing between 0 and N-1 types.
template <int N, typename TPack, typename First, typename... Others>
struct TypePackFirstN
{
    // sorry, unimplemented: cannot expand ‘Others ...’ into a fixed-length argument list
    typedef typename TypePackFirstN<N-1, typename TPack::template Add<First>::type, Others...>::type type;
};

// The stop condition for TypePackFirstN:  when N is 0, return the TypePack that has been built up.
template <typename TPack, typename... Others>
struct TypePackFirstN<0, TPack, Others...> //sorry, unimplemented: cannot expand ‘Others ...’ into a fixed-length argument list
{
    typedef TPack type;
};

EDIT: I've noticed that while a partial template instantiation that looks like does incur the error:

template <typename... T>
struct SomeStruct<1, 2, 3, T...> {};

Rewriting it as this does not produce an error:

template <typename... T>
struct SomeStruct<1, 2, 3, TypePack<T...>> {};

It seems that you can declare parameters to partial specializations to be variadic; i.e. this line is OK:

template <typename... T>

But you cannot actually use those parameter packs in the specialization, i.e. this part is not OK:

SomeStruct<1, 2, 3, T...>

The fact that you can make it work if you wrap the pack in some other type, i.e. like this:

SomeStruct<1, 2, 3, TypePack<T...>>

to me implies that the declaration of the variadic parameter to a partial template specialization was successful, and you just can't use it directly. Can anyone confirm this?

+3  A: 

What version of GCC are you using? According to this GCC status page, GCC 4.4 should support it.

Testing with GCC 4.4.2, I get similar error.

The wording of the error message seems to imply that the code should work according the C++0x standard, but that GCC doesn't support it yet. Or perhaps it is a compiler bug?

This is correct, GCC understands the code but cannot yet spit out GIMPLE for it.

As for what causes the error, it's the extension of template variable list to another template's list of variables.

LiraNuna
I'm using GCC 4.4.1.
Dennis
Could you explain more fully what it means to extend a template variable list into another template's list? I was beginning to think it had to do with not being able to use variadic template packs directly as arguments to a template specialization.
Dennis
If the pack expansion creates a template-argument list where some arguments hit fixed parameters, and some hit a template parameter pack, then this error does seem to occur. I rewrote your code to this, and it does work: http://codepad.org/IBT60IbJ (i had to introduce another partial specialization, to make the `<0, A, B>` case choose an uniq specialization). In this one, each pack expansion always hits a corresponding parameter pack only.
Johannes Schaub - litb
+5  A: 

There is a trick to get this to work with gcc. The feature isn't fully implemented yet, but you can structure the code to avoid the unimplemented sections. Manually expanding a variadic template into a parameter list won't work. But template specialization can do that for you.

template< char head, char ... rest >
struct head_broken
{
   static const char value = head;
};

template< char ... all >
struct head_works; // make the compiler hapy

template< char head, char ... rest >
struct head_works<head,rest...> // specialization
{
   static const char head = head;
};

template<char ... all >
struct do_head
{
   static const char head = head_works<all...>::value;
   //Sorry, unimplemented: cannot expand 'all...' into a fixed-length arugment list
   //static const char head = head_broken<all...>::value;
};

int main
{
   std::cout << head_works<'a','b','c','d'>::value << std::endl;
   std::cout << head_broken<'a','b','c','d'>::value << std::endl;
   std::cout << do_head<'a','b','c','d'>::head << std::endl;
}

I tested this with gcc 4.4.1

caspin
This should be actually the best answer. Specialization is the way to go with gcc 4.4.
Vlad Lazarenko
+1  A: 

As far as I understand the error is reported because the compiler sees the declaration of the template class as a class with at least 3 arguments followed by the optional ones. Since you try to refer to it with 2 arguments followed by the expansion list, it gets confused and issues this error. In order to make it compile correctly you just need to first declare the template like this:

template <int N, typename TPack, typename... Others>
struct TypePackFirstN;

And after that the recursion step definition has to be restated as a template specialization. (this does the trick with gcc 4.5.0 20100404).

// Takes the set (First, Others...) and, while N > 0, adds (First) to TPack.
// TPack is a TypePack containing between 0 and N-1 types.
template <int N, typename TPack, typename First, typename... Others>
struct TypePackFirstN<N, TPack, First, Others...>
{
    // Error "sorry, unimplemented: cannot expand ‘Others ...’ into a fixed-length argument list" should be now gone
    typedef typename TypePackFirstN<N-1, typename TPack::template Add<First>::type, Others...>::type type;
};

// The stop condition for TypePackFirstN:  when N is 0, return the TypePack that has been built up.
template <typename TPack, typename... Others>
struct TypePackFirstN<0, TPack, Others...>         // Error "sorry, unimplemented: cannot expand ‘Others ...’ into a fixed-length argument list" should be now gone
{
    typedef TPack type;
};
Martin Bektchiev