GCC error with variadic templates: “Sorry, unimplemented: cannot expand 'Identifier…' into a fixed-length argument list”

前端 未结 4 1204
太阳男子
太阳男子 2020-12-05 02:10

While doing variadic template programming in C++11 on GCC, once in a while I get an error that says \"Sorry, unimplemented: cannot expand \'Identifier...\' into a fixed-leng

4条回答
  •  粉色の甜心
    2020-12-05 02:58

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

提交回复
热议问题