无法使用元编程在变量模板中声明变量
Cannot declare a variable in a variadic template using metaprogramming
我使用一个可变模板函数为每个参数执行一个函数。迭代使用元编程,但有一个问题:我不能在其中声明一个简单的变量
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
(激活所有警告)。你的问题是你没有找到真正的错误是什么,你被编译器提供的模板扩展注释分散了注意力。
相关文章:
- 在将变量声明为引用时,堆在释放后使用
- 静态变量声明和定义
- 在变量声明中使用 for 循环
- 向量索引变量声明(size_t 或 std::vector<DATATYPE>::size_type)
- C++ 类型类的变量声明不命名类型?
- 将本地 OpenCV Mat 变量声明为静态以防止重新分配
- std::enable_if 更改成员 *变量* 声明/类型
- 在C++中,变量声明左侧的大括号是什么意思?
- 将变量声明为全局变量,然后声明为局部变量 -Shadowing-
- 命名空间内C++变量声明
- 将静态全局变量声明为内联有什么意义吗?
- 如何将一个变量声明为另一个变量的值
- 如何将迭代器变量声明为私有成员变量
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 有没有人有书面证据来保证函数中参数的定义方式与普通变量声明相同?
- ClangTool 如何获取变量声明中模板参数的位置?
- 标准与显式自动推导变量声明
- C 静态变量声明怪异链接器错误
- 如何为数组成员变量声明 getter/setter
- 可视C++变量声明