败犬日报 2025-11-12
败犬日报 2025-11-12
1. 微软大战代码 Microsoft VS Code(图)

2. 怎么编译期把类型为 tuple<int, ...> 的值转换成 struct { int[N] },以及反过来
类型体操来了。
cpp
#include <iostream>
template <typename T, std::size_t N>
struct fixed_array_wrapper {
T array[N];
};
template <typename T, typename... Ts>
concept all_same_as = (std::is_same_v<T, Ts> && ...);
template <typename... Ts, std::size_t... I>
constexpr auto tuple_to_array_impl(const std::tuple<Ts...>& tup,
std::index_sequence<I...>) {
static_assert(
all_same_as<std::tuple_element_t<0, std::tuple<Ts...>>, Ts...>,
"All tuple elements must have the same type");
using T = std::tuple_element_t<0, std::tuple<Ts...>>;
return fixed_array_wrapper<T, sizeof...(Ts)>{{std::get<I>(tup)...}};
}
template <typename... Ts>
constexpr auto tuple_to_array(const std::tuple<Ts...>& tup) {
return tuple_to_array_impl(tup, std::index_sequence_for<Ts...>{});
}
template <typename T, std::size_t N, std::size_t... I>
constexpr auto array_to_tuple_impl(const fixed_array_wrapper<T, N>& arr,
std::index_sequence<I...>) {
return std::make_tuple(arr.array[I]...);
}
template <typename T, std::size_t N>
constexpr auto array_to_tuple(const fixed_array_wrapper<T, N>& arr) {
return array_to_tuple_impl(arr, std::make_index_sequence<N>{});
}
int main() {
constexpr auto tuple = array_to_tuple(fixed_array_wrapper<int, 3>{1, 2, 3});
std::cout << std::get<0>(tuple) << " " << std::get<1>(tuple) << " "
<< std::get<2>(tuple) << std::endl;
constexpr auto array = tuple_to_array(std::tuple{4, 5, 6});
std::cout << array.array[0] << " " << array.array[1] << " "
<< array.array[2] << std::endl;
}