如何使用未扩展的参数包修复编译错误

时间:2019-08-14 20:59:43

标签: c++ templates

在展开未扩展的参数包时出现错误。

此代码用于测试。

稍后我想用head,tail和element调用一个different函数。 我需要该元素来调用另一个模板函数,并希望保留另一个参数以在以后调用另一个模板函数。

铛的当前输出(版本:8.0.0)

test.cpp

#include <iostream>

template <int... Tail>
class for_each_type {
    public:
    template <int element, int... Head>
    int each() {
        for_each_type<Tail..., element> _for;

        _for.each<Head...>();
        return element;
    }

    template <int element>
    int each() {
        return element;
    }
};

int main() {
    for_each_type<> _for;

    std::cout << _for.each<1, 1, 2, 2>() << std::endl;
}

我希望代码输出6,但实际输出是错误消息。

要运行启动编译器,我只需运行clang++ test.cpp

当前错误消息是:

C:\Users\HP\Documents\GitHub\CPP-Game-Engine\src>clang++ test.cpp
test.cpp:10:23: error: expected ';' after expression
        _for.each<Head...>();
                      ^
                      ;
test.cpp:10:9: error: expression contains unexpanded parameter pack 'Head'
        _for.each<Head...>();
        ^         ~~~~
test.cpp:10:23: error: expected expression
        _for.each<Head...>();
                      ^
3 errors generated.

0 个答案:

没有答案