designated initializers для шаблонного типа
#include <cstdint>
#include <cstddef>
#include <type_traits>
template <typename T>
inline constinit std::type_identity<T> ttag{};
template <typename Tint_type = decltype(ttag<uint64_t>)>
struct algo_config {
bool heuristic42 = true;
Tint_type int_type = ttag<uint64_t>;
size_t threads = 4ull;
};
template <typename ...Ts>
auto algo(auto data, algo_config<Ts...> const & cfg) {
}
int main() {
algo("data", {.int_type = ttag<uint32_t>, .threads = 8});
}
Почему код компилируется, если явно указать algo_config
, но без него получаю ошибку?
error: invalid initialization of reference of type 'const algo_config<>&' from expression of type '<brace-enclosed initializer list>'
Взято из этой статьи https://hannes.hauswedell.net/post/2023/03/30/algo_config/.
Источник: Stack Overflow на русском