http://en.cppreference.com/w/cpp/utility/tuple/make_tuple (为了方便起见,粘贴了代码)
#include <iostream>
#include <tuple>
#include <functional>
std::tuple<int, int> f() // this function returns multiple values
{
int x = 5;
return std::make_tuple(x, 7); // return {x,7}; in C++17
}
int main()
{
// heterogeneous tuple construction
int n = 1;
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
n = 7;
std::cout << "The value of t is " << "("
<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
<< std::get<4>(t) << ")\n";
// function returning multiple values
int a, b;
std::tie(a, b) = f();
std::cout << a << " " << b << "\n";
}
Run Code Online (Sandbox Code Playgroud)
https://theboostcpplibraries.com/boost.tuple
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <string>
#include <iostream>
int main()
{
typedef boost::tuple<std::string, int, bool> animal;
animal a = boost::make_tuple("cat", 4, true);
a.get<0>() = "dog";
std::cout << std::boolalpha << a << '\n';
}
Run Code Online (Sandbox Code Playgroud)
根据文档, boost::make_tuple 和 std::make_tuple 似乎是完全可以互换的。
它们真的完全可以互换吗?在什么情况下他们不是?
在 boost 文档中,它说 boost::tuple 和 std::tuple 在 c++11 中是相同的
在 std 文档中,它说 make_tuple 返回一个 std::tuple。
那么我是否遗漏了任何细微差别?
没有功能上的区别。
\nboost::tuple创建于近二十年前,并std::tuple于 2011 年引入 C++11 的核心标准库,距今仅 6 年。
对于“可互换”一词的给定定义,它们不是“可互换的”。您不能将 a 分配std::tuple<>给 a boost::tuple<>,反之亦然,因为即使它们的实现相同,它们仍然代表不同的对象。
但是,因为它们本质上是相同的,所以您可以对 \xe2\x86\x92 执行 find\xe2 boost::tuple\x86\ x92replace std::tuple,并且或多或少会以相同的行为和执行代码到达,并且因为对 boost 库的依赖不存在每个程序员都可以拥有的东西,几乎普遍建议任何可以访问 >=C++11 的项目std::tuple在所有情况下都更喜欢。
boost::tuple正如 @Nir 所指出的,和之间存在一些语法差异std::tuple,特别是涉及get<>()语法,它也是 的成员函数boost::tuple,并且只是 的自由函数std::tuple。
| 归档时间: |
|
| 查看次数: |
2012 次 |
| 最近记录: |