Today, I arrived at a situation, where I have a vector of tuples, where the tuples might contain several entries. Now I wanted to convert my vector of tuples to a vector of objects, such that the entries of the tuples will exactly match the uniform initialization of my object.
The following code does the job for me, but it is a bit clumsy. I'm asking myself if it might be possible to derive a generic solution that can construct …
我想从函数中返回一些值,我想将它打包在一个元组中.所以我有两种功能声明的可能性:
std::tuple<bool, string, int> f()
{
...
return std::make_tuple(false, "home", 0);
}
Run Code Online (Sandbox Code Playgroud)
和
std::tuple<bool, string, int> f()
{
...
return std::forward_as_tuple(false, "home", 0);
}
Run Code Online (Sandbox Code Playgroud)
这些功能是等价的?在您喜欢的这些功能之间?
这似乎是一个非常简单的问题:如何删除第一个(第n个)类型std::tuple?
例:
typedef std::tuple<int, short, double> tuple1;
typedef std::tuple<short, double> tuple2;
Run Code Online (Sandbox Code Playgroud)
上述操作将转变tuple1为tuple2.可能吗?
我的意思是为什么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
Run Code Online (Sandbox Code Playgroud)
但是,如上所述,对于许多其他类模板,您没有这样的函数.
考虑以下代码:
#include <utility>
#include <tuple>
std::pair<int, int> f1()
{
return std::make_pair(0x111, 0x222);
}
std::tuple<int, int> f2()
{
return std::make_tuple(0x111, 0x222);
}
Run Code Online (Sandbox Code Playgroud)
Clang 3和4在x86-64上生成类似的代码:
f1():
movabs rax,0x22200000111
ret
f2():
movabs rax,0x11100000222 ; opposite packing order, not important
ret
Run Code Online (Sandbox Code Playgroud)
但是Clang 5生成了不同的代码f2():
f2():
movabs rax,0x11100000222
mov QWORD PTR [rdi],rax
mov rax,rdi
ret
Run Code Online (Sandbox Code Playgroud)
正如GCC 4至GCC 7一样:
f2():
movabs rdx,0x11100000222
mov rax,rdi
mov QWORD PTR [rdi],rdx ; GCC 4-6 use 2 DWORD stores
ret
Run Code Online (Sandbox Code Playgroud)
返回std::tuple适合单个寄存器的生成代码为什么会更糟std::pair?看起来特别奇怪,因为Clang 3和4似乎是最优的,而5则不是.
在这里试试:https: …
标准表示std::tuple具有以下成员功能
constexpr tuple();
explicit tuple(const Types&...);
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下应该发生std::tuple<>什么吗?
此代码不能使用GCC4.7进行编译
struct A {};
void f(A);
struct B { B(std::tuple<A>); };
void f(B);
int main() {
f(std::make_tuple(A()));
}
Run Code Online (Sandbox Code Playgroud)
因为GCC派生自A利用空基类优化.然而,这导致海湾合作委员会挑选f(A)和抱怨
错误:
'A'是一个无法访问的基础'tuple<A>'
这个错误是由C++标准授予的,还是仅仅是libstdc ++的错误?
我想创建一个模板,我可以在其中输入索引,它将为我提供该索引的类型.我知道我可以这样做,decltype(std::get<N>(tup))但我想自己实现.例如,我想这样做,
typename get<N, std::tuple<int, bool, std::string>>::type;
Run Code Online (Sandbox Code Playgroud)
...它会给我位置的类型N - 1(因为数组从0开始索引).我怎样才能做到这一点?谢谢.
我正在考虑在元组/联系的情况下返回值优化,我观察到的行为并不像我预期的那样.在下面的例子中,我希望移动语义能够启动,但它有一个复制操作.以下优化的输出是:
Test duo output, non_reference tuple
Default constructor invoked
Parameter constructor invoked
Copy constructor invoked
Move Assignment operator invoked
100
Run Code Online (Sandbox Code Playgroud)
在函数内部创建元组时复制构造函数的调用似乎是不必要的.有什么办法可以删除吗?我正在使用MSVC 2012编译器.
#include <iostream>
#include <tuple>
class A
{
public:
int value;
A() : value(-1)
{
std::cout << "Default constructor invoked" << std::endl;
}
explicit A(const int v) : value(v)
{
std::cout << "Parameter constructor invoked" << std::endl;
}
A(const A& rhs)
{
value = rhs.value;
std::cout << "Copy constructor invoked" << std::endl;
}
A(const A&& rhs)
{
value = …Run Code Online (Sandbox Code Playgroud)