c++ - 为什么不使用 boost::tuple 的 .get 在 gcc 的模板函数中工作?

标签 c++ templates boost g++ tuples

在尝试移植一些代码以在 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/

相关文章:

c# - 从 c# 调用 c++ 函数指针

c++ - 编译器无法识别 Getline

c++ - <boost/lexcal_cast.hpp> 在哪里?

c++ - 无法输入字符,导致无限循环

c++ - 带有 static_assert 的漂亮 sfinae

c++ - 通过模板编译时父子关系

css - 创建一个包含三个帖子的 Blogger 模板

c++ - 从指向 boost::any 的指针调用可变参数模板

c++ - 在哪里可以找到 libboost_random?

c++ - 如果我将文件扩展名更改为 .cpp, native 事件示例项目将无法编译