现有元组重载std::get仅限于按索引或类型返回 1 个元素。想象一下,有一个包含多个相同类型元素的元组,并且您希望将它们全部提取到一个新元组中。
std::get<T>如何实现像这样返回给std::tuple定类型的所有出现的版本?
template<typename... Ts_out>
constexpr std::tuple<Ts_out...> extract_from_tuple(auto& tuple) {
// fails in case of multiple occurences of a type in tuple
return std::tuple<Ts_out...> {std::get<Ts_out>(tuple)...};
}
auto tuple = std::make_tuple(1, 2, 3, 'a', 'b', 'c', 1.2, 2.3, 4.5f);
auto extract = extract_from_tuple <float, double>(tuple);
// expecting extract == std::tuple<float, double, double>{4.5f, 1.2, 2.3}
Run Code Online (Sandbox Code Playgroud)
不确定按std::make_index_sequence元素访问每个元素是否可行。std::get<index>std::is_same_v
如何使用参数(例如另一个元组)为类型列表中的每种类型调用模板函数?
给定的是一个类型列表std::tuple<T1, T2, T3, ...>和一个std::tuple包含数据。
template <typename T>
void doSomething (const auto& arg) {
std::cout << __PRETTY_FUNCTION__ << '\n';
}
template <typename T> struct w {T v; w(T _v) : v{_v} {}};
int main () {
using types = std::tuple<int, char, float, double, w<int>, w<float>>; // used as type list
constexpr auto data = std::make_tuple(1, 2, 3.0, 4.0f, w(5.0));
// call doSomething<T>(data) for each type in types
// like
// someFunctor<types>(doSomething, data);
}
Run Code Online (Sandbox Code Playgroud)
我当前的想法是一个类似函子的应用程序,它接收类型列表以提取下一个类型,并对每个 Tstd::tuple<Ts> …
我想在类中使用静态方法来获取表单中的类型列表std::tuple<T1, T2, T3,...>。我不想与 一起工作,而是std::tuple<...>想拥有<...>. 如何实现示例struct x结果Ts == <T1, T2, T3,...>
template<template <typename...> typename TL, typename... Ts>
struct x {
static void test() {
// expecting Ts == <int, char, float, double> (without std::tuple<>)
std::cout << __PRETTY_FUNCTION__ << '\n';
}
};
Run Code Online (Sandbox Code Playgroud)
using types = std::tuple<int, char, float, double>;
x<types>::test();
Run Code Online (Sandbox Code Playgroud)
请参阅godbolt 上的示例
c++ templates variadic-templates template-argument-deduction c++20
如何通过索引(例如std::tuple_element )(最好以非递归方式)using L = type_list<T1, T2, ...>检索类型列表的元素?
我想避免使用元组作为用例的类型列表,这需要实例化来传递像f(L{}).
template<typename...> struct type_list {};
using L = typelist<int, char, float, double>;
using T = typeAt<2, L>; // possible use case
Run Code Online (Sandbox Code Playgroud)
不确定使用std::index_sequence进行迭代并通过索引版本 的std::is_same进行测试是否是一个好的方法。std::integral_constant
给定 2 个位掩码,应交替访问(0,1,0,1...)。我尝试获得运行时高效的解决方案,但找不到比以下示例更好的方法。
uint32_t mask[2] { ... };
uint8_t mask_index = 0;
uint32_t f = _tzcnt_u32(mask[mask_index]);
while (f < 32) {
// element adding to result vector removed, since not relevant for question itself
mask[0] >>= f + 1;
mask[1] >>= f + 1;
mask_index ^= 1;
f = _tzcnt_u32(mask[mask_index]);
}
Run Code Online (Sandbox Code Playgroud)
ASM 输出(MSVC、x64)似乎被放大了很多。
inc r9
add r9,rcx
mov eax,esi
mov qword ptr [rdi+rax*8],r9
inc esi
lea rax,[rcx+1]
shrx r11d,r11d,eax
mov dword ptr [rbp],r11d
shrx r8d,r8d,eax
mov dword ptr [rbp+4],r8d
xor …Run Code Online (Sandbox Code Playgroud) 如何调用 a ,以constexpr 方式static constexpr class::method (int i1, int i2, int i3)提供输入数据。tuple<int, int, int>
默认方法是使用std::apply将每个元组元素作为参数应用于函数。
一个最小的可视化示例,我试图实现的目标如下:
struct a {
template <typename T>
static constexpr void test(int i1, int i2, int i3) {
// ...
}
};
struct b : a {};
struct c {};
template <typename T>
struct test_functor {
constexpr test_functor_t() {} // just for testing to express constexpr desire
constexpr void operator()(auto... args) {
T::test<c>(args...);
}
};
constexpr std::tuple<int, int, int> tupl{ 1,2,3 …Run Code Online (Sandbox Code Playgroud) 我想通过constexpr constructor a存储传递的数据struct,并将数据存储在 a 中std::tuple,以执行各种 TMP/编译时操作。
执行
template <typename... _Ts>
struct myInitializer {
std::tuple<_Ts...> init_data;
constexpr myInitializer(_Ts&&... _Vs)
: init_data{ std::tuple(std::forward<_Ts>(_Vs)...) }
{}
};
Run Code Online (Sandbox Code Playgroud)
存储的数据使用轻量级strong type结构,通过左值和右值帮助器重载生成:
template <typename T, typename... Ts>
struct data_of_t {
using type = T;
using data_t = std::tuple<Ts...>;
data_t data;
constexpr data_of_t(Ts&&... _vs)
: data(std::forward<Ts>(_vs)...)
{}
};
template<typename T, typename... Ts>
constexpr auto data_of(Ts&&... _vs) {
return data_of_t<T, Ts...>(std::forward<Ts>(_vs)...);
};
template<typename T, typename... Ts>
constexpr auto data_of(Ts&... _vs) { …Run Code Online (Sandbox Code Playgroud) c++ ×7
c++20 ×6
constexpr ×4
tuples ×3
optimization ×1
performance ×1
template-argument-deduction ×1
templates ×1
x86-64 ×1