似乎 std::get
只用在元组类上。为什么不把它做成标准库中元组的成员类,还有别的用法吗?
最佳答案
The reason get is a non-member function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the template keyword.
source .
get
为非成员函数时的代码片段:
template<class T>
void foo ( tuple<T>& t ) {
get<0>(t) = 10; // get is non-member function
}
还有一个如果get
是元组的成员函数:
template<class T>
void foo ( tuple<T>& t ) {
t. template get<0>() = 10; // ugly
}
您更喜欢哪个版本的get
用法?对我来说,第一个更好。
关于function - 为什么c++11将get<>(tuple)定义为全局函数而不是tuple的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52301517/