我的意思是为什么std::make_tuple
存在?我知道在某些情况下,该函数会减少您必须输入的字符数量,因为您可以避免使用模板参数。但这是唯一的原因吗?std::tuple
该函数存在而其他类模板没有这样的功能有什么特别之处?仅仅是因为std::tuple
在这种情况下您可能会更频繁地使用吗?
以下是std::make_tuple
减少字符数量的两个示例:
// Avoiding template parameters in definition of variable.
// Consider that template parameters can be very long sometimes.
std::tuple<int, double> t(0, 0.0); // without std::make_tuple
auto t = std::make_tuple(0, 0.0); // with std::make_tuple
// Avoiding template parameters at construction.
f(std::tuple<int, double>(0, 0.0)); // without std::make_tuple
f(std::make_tuple(0, 0.0)); // with std::make_tuple
但是就像上面写的那样,对于许多其他类模板,您没有这样的功能。