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&
的函数;但是您的版本必须根据其在运行时指定的参数返回不同的类型,这是不可能的。
关于c++ - 为什么 std::get 使用模板参数而不是常规参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284862/