之前的问题和答案(此处)显示可以通过以下方式创建元组列表:
#include <vector>
#include <boost/tuple/tuple.hpp>
using namespace std;
using boost::tuple;
typedef vector< tuple<int, int> > tuple_list;
Run Code Online (Sandbox Code Playgroud)
虽然在使用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;
^
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?(如果我可以对我想要的另一个帖子发表评论,但很棒的SO说我的声誉太低而无法发表评论!)
问题是名称冲突,您using boost::tuple和namespace 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;
}
Run Code Online (Sandbox Code Playgroud)
我想这是一个很好的例子,为什么using不太推荐;)