元组列表c++ 11/1y

C++: List of tuples C++11/1y

本文关键字:1y c++ 列表 元组      更新时间:2023-10-16

前面的一个问答(这里)表明,可以通过以下方式创建一个元组列表:

#include <vector>
#include <boost/tuple/tuple.hpp>
using namespace std;
using boost::tuple;
typedef vector< tuple<int, int> > tuple_list;

当我在c++ 98运行时没有得到错误,c++ 1y (Ubuntu上的GCC/GNU)给出:

error: template argument 1 is invalid
typedef vector< tuple<int, int> > tuple_list;
                                ^
error: template argument 2 is invalid
error: invalid type in declaration before ‘;’ token
typedef vector< tuple<int, int> > tuple_list;
                                            ^

知道是怎么回事吗?(如果我能在其他帖子上发表评论,我会的,但是awesome SO说我的声誉太低了,不能发表评论!)

问题是名称冲突,您是using boost::tuplenamespace std;,两者都将tuple带入全局作用域,因此您最终得到同一模板的两个定义。我不明白为什么编译器在诊断错误时不更明确…

删除using boost::tuple;using namespace std;并限定相应的名称:

#include <vector>
#include <boost/tuple/tuple.hpp>
//using namespace std;
//using boost::tuple;
typedef std::vector< boost::tuple<int, int> > tuple_list;
int main()
{
    tuple_list foo;
}

我想这是一个很好的例子,为什么using不太推荐;)