标签: stdtuple

Uniform initialization by tuple

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 …

c++ stl stl-algorithm uniform-initialization stdtuple

26
推荐指数
3
解决办法
679
查看次数

在C++ 11中从函数返回元组的最佳方法是什么?

我想从函数中返回一些值,我想将它打包在一个元组中.所以我有两种功能声明的可能性:

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)

这些功能是等价的?在您喜欢的这些功能之间?

c++ c++11 stdtuple

25
推荐指数
2
解决办法
8743
查看次数

删除第一种类型的std :: tuple

这似乎是一个非常简单的问题:如何删除第一个(第n个)类型std::tuple

例:

typedef std::tuple<int, short, double> tuple1;
typedef std::tuple<short, double> tuple2;
Run Code Online (Sandbox Code Playgroud)

上述操作将转变tuple1tuple2.可能吗?

c++ c++11 stdtuple

24
推荐指数
2
解决办法
4846
查看次数

`std :: make_tuple`的原因是什么?

我的意思是为什么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)

但是,如上所述,对于许多其他类模板,您没有这样的函数.

c++ c++11 stdtuple

24
推荐指数
2
解决办法
7127
查看次数

为什么对std :: tuple实现使用递归继承不好?

这个问题上,Howard Hinnant说

std :: tuple的一些实现使用递归继承.但好的却没有.;-)

有人可以对此有所了解吗?

c++ c++11 stdtuple

23
推荐指数
1
解决办法
2880
查看次数

返回比std :: pair更低效的2元组?

考虑以下代码:

#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: …

c++ gcc clang calling-convention stdtuple

21
推荐指数
1
解决办法
1474
查看次数

在ISO C++标准中由std :: tuple的默认构造函数描述混淆

标准表示std::tuple具有以下成员功能

constexpr tuple();
explicit tuple(const Types&...);
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下应该发生std::tuple<>什么吗?

c++ language-lawyer c++11 stdtuple

18
推荐指数
2
解决办法
2007
查看次数

是否允许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 ++的错误?

c++ language-lawyer c++11 stdtuple

18
推荐指数
1
解决办法
364
查看次数

如何从元组中获得第N类?

我想创建一个模板,我可以在其中输入索引,它将为我提供该索引的类型.我知道我可以这样做,decltype(std::get<N>(tup))但我想自己实现.例如,我想这样做,

typename get<N, std::tuple<int, bool, std::string>>::type;
Run Code Online (Sandbox Code Playgroud)

...它会给我位置的类型N - 1(因为数组从0开始索引).我怎样才能做到这一点?谢谢.

c++ templates c++11 stdtuple

17
推荐指数
2
解决办法
3215
查看次数

元组/领带的返回值优化

我正在考虑在元组/联系的情况下返回值优化,我观察到的行为并不像我预期的那样.在下面的例子中,我希望移动语义能够启动,但它有一个复制操作.以下优化的输出是:

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)

c++ rvo c++11 stdtuple

17
推荐指数
2
解决办法
3166
查看次数