gpt4 book ai didi

function - 为什么c++11将get<>(tuple)定义为全局函数而不是tuple的成员?

转载 作者:行者123 更新时间:2023-12-05 02:15:10 24 4
gpt4 key购买 nike

似乎 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com