无法使用元编程在变量模板中声明变量

Cannot declare a variable in a variadic template using metaprogramming

本文关键字:变量 声明 编程      更新时间:2023-10-16

我使用一个可变模板函数为每个参数执行一个函数。迭代使用元编程,但有一个问题:我不能在其中声明一个简单的变量

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args... > const& t )
    -> typename std::enable_if< I == sizeof...(Args), void >::type
{
}

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args...>const & t )
    -> typename std::enable_if< I < sizeof...(Args), void >::type
{
    cout << get<I>(t) << endl;
    recursive_function< I + 1, Args... >( t );
}

这些函数可以很好地显示任何参数,但如果我们稍微更改代码。。。

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args... > const& t )
    -> typename std::enable_if< I == sizeof...(Args), void >::type
{
}

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args...>const & t )
    -> typename std::enable_if< I < sizeof...(Args), void >::type
{
    cout << get<I>(t) << endl;
    // Just a simple declaration added...
    int i = 0;
    recursive_function< I + 1, Args... >( t );
}

[编辑]编译器抱怨:

main.cpp: In instantiation of 'typename std::enable_if<(I < sizeof (Tp ...)), void>::type recursive_function(const std::tuple<_Elements ...>&) [with long unsigned int I = 0ul; Args = {int, int, int, int, int}; typename std::enable_if<(I < sizeof (Tp ...)), void>::type = void]':
main.cpp:524:31:   required from here
main.cpp:511:9: warning: unused variable 'i' [-Wunused-variable]
 int i=0;
     ^
main.cpp: In instantiation of 'typename std::enable_if<(I < sizeof (Tp ...)), void>::type recursive_function(const std::tuple<_Elements ...>&) [with long unsigned int I = 1ul; Args = {int, int, int, int, int}; typename std::enable_if<(I < sizeof (Tp ...)), void>::type = void]':
main.cpp:512:45:   required from 'typename std::enable_if<(I < sizeof (Tp ...)), void>::type recursive_function(const std::tuple<_Elements ...>&) [with long unsigned int I = 0ul; Args = {int, int, int, int, int}; typename std::enable_if<(I < sizeof (Tp ...)), void>::type = void]'
main.cpp:524:31:   required from here
main.cpp:511:9: warning: unused variable 'i' [-Wunused-variable]
main.cpp: In instantiation of 'typename std::enable_if<(I < sizeof (Tp ...)), void>::type recursive_function(const std::tuple<_Elements ...>&) [with long unsigned int I = 2ul; Args = {int, int, int, int, int}; typename std::enable_if<(I < sizeof (Tp ...)), void>::type = void]':
main.cpp:512:45:   recursively required from 'typename std::enable_if<(I < sizeof (Tp ...)), void>::type recursive_function(const std::tuple<_Elements ...>&) [with long unsigned int I = 1ul; Args = {int, int, int, int, int}; typename std::enable_if<(I < sizeof (Tp ...)), void>::type = void]'

这只是一个例子(没有理由以这种方式声明变量…),但我想了解这个错误的含义。

有人有主意吗?

[EDIT2]这是工作代码

#include <iostream>
#include <tuple>
#include <utility>

using namespace std;

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args... > const& t )
    -> typename std::enable_if< I == sizeof...(Args), void >::type
{
}

template < size_t I = 0, typename... Args >
auto recursive_function( std::tuple< Args...>const & t )
    -> typename std::enable_if< I < sizeof...(Args), void >::type
{
    cout << get<I>(t) << endl;
    int i = 0;
    recursive_function< I + 1, Args... >( t );
}

int main( int argc, char** argv )
{
    try
    {
        auto t = make_tuple(1,2,3,4,5);
        recursive_function( t );
    }
    catch( std::exception& e )
    {
        cerr << e.what() << endl;
    }
    return 0;
}

上面是整个错误消息。使用的编译器版本是GCC 4.8.2,选项std=c++1y,我使用的是Code::Blocks。奇怪的是,构建完成时出现了2个错误和15个警告,但实际输出考虑了更改(因此不考虑错误?)

您创建了一个变量,但没有使用它。

您启用了所有警告(-Wall),并启用了作为错误的警告。由于创建一个毫无意义的变量通常是你犯了错误的标志,编译器对此有警告

因此,一个警告(您创建了一个变量,但没有使用它)生成了一个错误,然后它详细说明了这个错误是如何发生的(模板实例化将其实例化包装为未使用的变量)。

在下一行插入(void)i;,警告就会消失。

请注意,将警告视为错误是一个好计划,请保持打开状态,并保持打开-Wall(激活所有警告)。你的问题是你没有找到真正的错误是什么,你被编译器提供的模板扩展注释分散了注意力。