- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
而不是像这样创建 vector :
std::vector<int> v1{1,2,3};
std::vector<double> v2{1.1,2.2,3.3};
std::vector<Object> v3{Object{},Object{},Object{}};
我想用通用函数创建它们:
auto v1 = make_vector(1,2,3);
auto v2 = make_vector(1.1,2.2,3.3);
auto v3 = make_vector(Object{},Object{},Object{});
类似于std::make_pair和 std::make_tuple ,这是我对 vector 的尝试:
#include <iostream>
#include <vector>
#include <utility>
template <typename... T>
auto make_vector(T&&... args)
{
using first_type = typename std::tuple_element<0, std::tuple<T...>>::type;
return std::vector<first_type>{std::forward<T>(args)...};
}
它可以编译,但是当我尝试使用它时:
auto vec = make_vector(1,2,3);
m.cpp: In instantiation of ‘auto make_vector(T&& ...) [with T = {int, int, int}]’:
m.cpp:16:30: required from here
m.cpp:8:78: error: invalid use of incomplete type ‘class std::tuple_element<0ul, std::tuple<int, int, int> >’
using first_type = typename std::tuple_element<0, std::tuple<T...>>::type;
^
In file included from m.cpp:3:0:
/usr/include/c++/5/utility:85:11: note: declaration of ‘class std::tuple_element<0ul, std::tuple<int, int, int> >’
class tuple_element;
^
m.cpp:9:60: error: invalid use of incomplete type ‘class std::tuple_element<0ul, std::tuple<int, int, int> >’
return std::vector<first_type>{std::forward<T>(args)...};
^
In file included from m.cpp:3:0:
/usr/include/c++/5/utility:85:11: note: declaration of ‘class std::tuple_element<0ul, std::tuple<int, int, int> >’
class tuple_element;
^
m.cpp: In function ‘int main()’:
m.cpp:16:30: error: ‘void v1’ has incomplete type
auto v1 = make_vector(1,2,3);
我怎样才能制作一个通用例程,
那个使用第一个参数的第一个类型来实例化vector?
如何将参数作为初始化值转发给 vector ?
最佳答案
因为无论如何你都不能用它来创建一个空 vector ,我们可以通过提供一个额外的模板参数来避免 tuple
依赖:
template <class T0, class... Ts>
auto make_vector(T0&& first, Ts&&... args)
{
using first_type = std::decay_t<T0>;
return std::vector<first_type>{
std::forward<T0>(first),
std::forward<Ts>(args)...
};
}
如果 first
作为左值传入,它还有一个额外的好处。
关于c++ - 如何编写类似于 std::make_tuple 的 make_vector?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994727/
http://en.cppreference.com/w/cpp/utility/tuple/make_tuple(为了方便粘贴代码) #include #include #include st
我正在玩这个小片段: #include struct copy_only { copy_only() = default; copy_only(copy_only&&) = dele
如下所示,为什么这里的“vv”不是右值引用?这里“v”的类型是右值引用,根据decltype的推理规则, decltype右值引用(不是纯右值)的类型应该是右值引用的类型,对吗? int main(i
这该怎么做: #include std::tuple f() { return std::make_tuple(0., {1, 2}); } int main() { f();
要理解问题,请阅读 this answer首先。 我检查了不同的历史 make_tuple 实现(包括 2012 年的 clang 版本)。在 C++17 之前,我希望它们返回 {list of va
我可以写make_tuple(1,true)或写 make_tuple(1,true)编译器会推断出它的类型。这种能力是否适用于我编写的代码,或者它是否以某种方式内置到我无法访问的编译器中? 最佳答案
有没有理由std::make_tuple不接受列表初始化对象的参数? #include #include #include class FileInfo { public: FileIn
考虑这段代码: #include int main() { int i; long k; auto tup1 = std::make_tuple(i); // Compi
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Implementing comparision operators via 'tuple' and 'ti
我有一个类将大小为 5 的数组作为构造函数中的参数之一。 class sha1 { public: typedef unsigned int(&type)[5]; type diges
我想获得 std::make_tuple 为给定参数包返回的类型。到目前为止,我已经编写了以下代码: #include #include template struct unwrap_refwr
我的代码中有这样的东西 val = boost::make_tuple(objA , objB); 我的问题是 boost::make_tuple 会复制 objA 和 objB 吗? 最佳答案 是的
我的意思是为什么 std::make_tuple 存在?我知道在某些情况下,该函数会减少您必须输入的字符数量,因为您可以避免使用模板参数。但这是唯一的原因吗?是什么让 std::tuple 函数存在而
我一直在尝试 std::tuple结合引用文献: #include #include int main() { int a,b; std::tuple test(a,b); std::
Here 指出 C++17 中的演绎指南将使 std::make_tuple 过时。然而,据我所知,std::make_tuple 和 std::tuple::tuple 的标准推导指南之间的区别在于
我假设由 std::make_tuple() 创建的元组对象是在堆栈上动态分配的。我的问题是当它的生命周期达到结束状态时如何释放/删除这个对象。 std::tuple myTuple = std::m
我正在尝试在 constexpr 中使用 make_tuple()。它在全局范围内工作。但它会为 static constexpr 类成员生成链接错误。 #include #include usi
是否有适用于 C#/.NET 的 make_tuple 等效项?我想做这样的事情 mylist.Add(new Tuple("", 1)); 当我创建自己的函数时,C# 似乎支持它 Tuple mak
如果构造函数的执行顺序很重要,我该如何使用 std::make_tuple? 比如我猜A类的构造函数和B类的构造函数的执行顺序是未定义的: std::tuple t(std::make_tuple(A
我一直在搜索这个主题,但没有找到任何令人满意的答案。我正在寻找一种方法来做大多数高级语言已经可以做的事情,采用一个元组和一个映射方法并返回一个映射的(也称为转换的)元组。理想情况下,这将适用于 c++
我是一名优秀的程序员,十分优秀!