在尝试移植一些代码以在 Linux 中编译时,我遇到了特殊的编译错误。通过搜索代码库,我终于设法将其归结为以下代码。
5: // include and using statements
6: template<typename RT, typename T1>
7: RT func(tuple<T1> const& t) {
8: return t.get<0>();
9: }
10: // test code
尝试使用它时出现错误:
test.cpp: In function <functionName>:
test.cpp:8: error: expected primary-expression before ‘)’ token
该代码在 Visual Studio 中运行良好,但出于某种原因我无法弄清楚为什么它不适用于 g++。这里有人知道如何解决这个问题吗?
最佳答案
你需要一些模板
love:
return t.template get<0>();
Visual C++ 无法正确解析模板,这就是它错误地接受没有 template
关键字的代码的原因。有关为什么此处需要 template
的更多信息,请参阅 Stack Overflow C++ FAQ "Where and why do I have to put “template” and “typename” on dependent names?"
关于c++ - 为什么不使用 boost::tuple 的 .get 在 gcc 的模板函数中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311459/