我试图std::tie()按照以下代码交换两个变量(我知道std::swap,我只是出于好奇而尝试这个):
#include <iostream>
#include <tuple>
using std::cin; using std::tie;
using std::cout; using std::endl;
using std::make_pair;
int main() {
int a = 2, b = 10;
cout << "Before Swapping using {}" << endl;
cout << "a: " << a << " b: " << b < <endl;
tie(a, b) = {b, a};
cout << "After Swapping using {}" << endl;
cout << "a: " << a << " b: " << b << endl;
a = …Run Code Online (Sandbox Code Playgroud) 我想实现一个带有仿tuple_map函数的泛型函数,并将仿函数std::tuple应用于此元组的每个元素并返回std::tuple结果.实现非常简单,但问题出现了:这个函数应返回什么类型?我的实现使用了std::make_tuple.然而,在这里 std::forward_as_tuple建议.
更具体地说,实现(为简洁起见,省略了对空元组的处理):
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
template<class Fn, class Tuple, std::size_t... indices>
constexpr auto tuple_map_v(Fn fn, Tuple&& tuple, std::index_sequence<indices...>)
{
return std::make_tuple(fn(std::get<indices>(std::forward<Tuple>(tuple)))...);
// ^^^
}
template<class Fn, class Tuple, std::size_t... indices>
constexpr auto tuple_map_r(Fn fn, Tuple&& tuple, std::index_sequence<indices...>)
{
return std::forward_as_tuple(fn(std::get<indices>(std::forward<Tuple>(tuple)))...);
// ^^^
}
template<class Tuple, class Fn>
constexpr auto tuple_map_v(Fn fn, Tuple&& tuple)
{
return tuple_map_v(fn, std::forward<Tuple>(tuple),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
template<class Tuple, class Fn>
constexpr …Run Code Online (Sandbox Code Playgroud) I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is:
A
Bye
B
C
Bye
Bye
So presumably one temporary is being created.
#include <iostream>
#include <tuple>
struct INeedElision{
int i;
~INeedElision(){
std::cout << "Bye\n";
}
};
std::tuple<int, INeedElision> f(){
int i = 47;
return {i, {47}};
}
INeedElision g(){
return {};
}
int main()
{
std::cout << "A\n";
auto x = …Run Code Online (Sandbox Code Playgroud) #include <iostream>
class NoCopyMove {
public:
NoCopyMove(int a) : a_(a), b_(a) {}
NoCopyMove(int a, int b) : a_(a), b_(b) {}
NoCopyMove(const NoCopyMove&) = delete;
NoCopyMove& operator=(const NoCopyMove&) = delete;
NoCopyMove(NoCopyMove&&) = delete;
NoCopyMove& operator=(NoCopyMove&&) = delete;
int a_;
int b_;
};
int main()
{
std::tuple<NoCopyMove, NoCopyMove> t {6, 9};
std::cout << std::get<0>(t).a_ << std::endl;
std::tuple<NoCopyMove, NoCopyMove> t2 {{6, 7}, {8, 9}};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个具有两个以上参数作为构造函数的类元组。如果只有一个构造函数参数,它就可以工作。
main.cpp:45:28: error: no matching constructor for initialization of 'std::tuple<NoCopyMove>'
std::tuple<NoCopyMove> t2 {{6, 7}, {8, 9}}}; …Run Code Online (Sandbox Code Playgroud) 我必须专门支持std :: get的模板是什么?
struct MyClass {
int a;
};
template <const size_t I>
struct MyContainer {
MyClass array[I];
};
Run Code Online (Sandbox Code Playgroud)
我有什么专业能够做到的:
MyContainer<16> mc;
std::get<0>(mc);
Run Code Online (Sandbox Code Playgroud) 我有一个复杂的类型,C取决于我在(长度有界)序列中需要的模板参数.constexpr函数next()可用于从C_n - > C_n + 1开始.由于每个序列元素都有不同的类型,我使用a std::tuple来存储结果.这些mkTuple()函数负责(有限的)序列推出.
这是我所做的一个简化示例(使用std::array占位符表示我更复杂的C):
#include <array>
#include <tuple>
#include <iostream>
template<std::size_t OFFSET>
using C=std::array<uint8_t, OFFSET>;
static
constexpr
std::size_t
next(const std::size_t START, const std::size_t DISTANCE)
{
return START + DISTANCE;
}
template<template<std::size_t OFFSET> class CONTENT, std::size_t START, std::size_t DISTANCE, std::size_t SEQ_LENGTH>
struct mkTuple
{
using _LT = CONTENT<START>;
using _RT = typename mkTuple<CONTENT, next(START, DISTANCE), DISTANCE, SEQ_LENGTH - 1>::tuple;
using tuple=decltype(std::tuple_cat(std::make_tuple(_LT()), _RT()));
};
template<template<std::size_t OFFSET> class CONTENT, std::size_t …Run Code Online (Sandbox Code Playgroud) 在许多单元测试中,我需要比较仅具有数据成员的简单结构的内容:
struct Object {
int start;
int stop;
std::string message;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我想写一些类似的东西:
CHECK(object1==object2);
Run Code Online (Sandbox Code Playgroud)
我总是必须实现:
bool operator==(const Object& lhs, const Object& rhs) {
return lhs.start==rhs.start && lhs.stop==rhs.stop && lhs.message=rhs.message;
}
Run Code Online (Sandbox Code Playgroud)
Writing all these comparison functions becomes tedious, but is also prone to errors. Just imagine, what will happen if I add a new data member to Object, but the comparison operator will not be updated.
Then I remembered my knowledge in Haskell and the magic deriving(Eq) directive, which just generates a …
在c ++中,如何使用指示元组长度的int模板参数实现一个函数并生成一个具有该长度的std :: tuple?
例如
func<2>() returns std::tuple<int, int>();
func<5>() returns std::tuple<int, int, int, int, int>().
Run Code Online (Sandbox Code Playgroud) 我的基本想法是从std :: tuple派生我自己的类,以获得一些内部帮助器类型:
template <typename ... T>
class TypeContainer: public std::tuple<T...>
{
public:
using BaseType = std::tuple<T...>;
static const size_t Size = sizeof...(T);
TypeContainer(T... args):std::tuple<T...>(args...){};
using index_sequence = std::index_sequence_for<T...>;
};
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用如下代码:
using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple<int>>;
using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<>, TypeContainer<int>>;
using MyType_tuple_non_empty = std::tuple< std::tuple<float,int>, std::tuple<int>, std::tuple<int>>;
using MyType_typecontainer_non_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<int>, TypeContainer<int>>;
template <typename T>
void Do( const T& parms )
{
// The following lines result in errors if TypeContainer with
// …Run Code Online (Sandbox Code Playgroud) 在c ++ 11中,std :: tie允许隐式转换吗?
下面的代码编译并运行,但我不确定幕后发生了什么,或者这是否安全.
std::tuple<float,float> foo() { return std::make_tuple(0,0); }
double a, b;
std::tie(a,b) = foo(); // a and b are doubles but foo() returns floats
Run Code Online (Sandbox Code Playgroud)