该std::get
函数使用模板参数作为常规参数。
举个例子:
std::tuple <int, double, long &, const char *> bar(18, 6.5, 12, "Hello!");
cout << std::get<2>(bar); // Print the tuple’s third element.
为什么此功能采用这种方式设计?
为什么不使用常规参数呢?
例如,类似:std::get(bar, 2)
或std::get(2, bar)
函数必须具有在编译时指定的定义良好的返回类型。这里的模板特化get<2>
是一个返回的函数long&
;但您的版本必须根据运行时指定的参数返回不同的类型,这是不可能的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句