Why doesn't using boost::tuple's .get work in template functions in gcc?

懵懂的女人 提交于 2020-01-03 08:53:23

问题


While trying to port some code to compile in linux I get peculiar compilation errors. Searching through the codebase I finally manage to get it down to the following code.

 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

Trying to use it I get the error:

test.cpp: In function <functionName>:
test.cpp:8: error: expected primary-expression before ‘)’ token

The code works fine in Visual Studio but for some reason I can't figure out why it doesn't work with g++. Anyone here got a clue how on how to work around this?


回答1:


You need some template love:

return t.template get<0>();

Visual C++ does not parse templates correctly, which is why it incorrectly accepts the code without the template keyword. For more information on why the template is required here, see the Stack Overflow C++ FAQ "Where and why do I have to put “template” and “typename” on dependent names?"



来源:https://stackoverflow.com/questions/6311459/why-doesnt-using-boosttuples-get-work-in-template-functions-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!