开发者

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

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
开发者_Python百科

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?


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?"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜