【发布时间】:2021-10-07 08:03:13
【问题描述】:
我希望在 C++20 中执行以下操作:
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator( )...;
};
// Updated to work with C++17
#if (_cplusplus != 202002L) // check for C++17 or C++20
// Deduction guide, google `CTAD for aggregates` for more info
template <typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>; // not needed before C++20
#endif
template <typename T, long int C = 0>
void emit(T const& data) {
auto emit = overloaded {
[&](const auto& value) {
mOs << value;
},
[&](const uint8_t& value) {
mOs << std::showbase << (uint16_t)value;
},
[&](const std::array<T, C>& value) {
for (auto& v : value) { // error: can't increment 0 size std::array
mOs << v;
}
},
// bunch more lambdas
};
emit(data);
}
// invoked by
emit(1);
既然需要计数,如何捕获 std::array?
如果不将 C 设置为零,所有其他 lambdas 都会失败。
可能不可能,但我想我会问的。
【问题讨论】: