Are tuples of tuples allowed?

限于喜欢 提交于 2019-12-23 07:58:05

问题


I'm currently working on a class with a lot of templates and being able to build tuples of tuples would make it a lot easier

But I tried this simple code in MSVC++ 2010:

#include <tuple>

void main() {
     auto x = std::make_tuple(std::make_tuple(5, true));
}

And I get a compilation error. The same problem happens if I don't use std::make_tuple but directly std::tuple's constructor.

Is it a bug of MSVC or are tuples of tuples not allowed by the standard?


回答1:


More data points:

  • If we use std::tr1::tuple and explicitly state the type instead of using auto, then Visual C++ 2008 compiles the code without error. Trying to compile that same code with Visual C++ 2010 results in the error you are seeing.

  • If we use boost::tuple an explicitly state the type instead of using auto, then Visual C++ 2008 and Visual C++ 2010 both compile the code without error.

It looks like it is probably an implementation bug.




回答2:


You've making a tuple with only one member --- doesn't that defeat the purpose of tuples? Anyway, I suspect that's leading to ambiguity.

make_tuple combines type inference with a call to the tuple constructor. When the tuple constructor is called with a single argument which is also a tuple, it's possible that a converting constructor is a better fit than a wrapping constructor. Hence the problem.

Tuples of tuples are allowed. 1-Tuples might not be.



来源:https://stackoverflow.com/questions/3254427/are-tuples-of-tuples-allowed

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