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/.
Ответы (1 шт):
Автор решения: HolyBlackCat
→ Ссылка
Судя по всему, аргументы функций вида {...} никогда не участвуют в выводе шаблонных аргументов функций.
Поэтому typename ...Ts выводится как пустой список, и у typename Tint_type срабатывает аргумент по умолчанию (decltype(ttag<uint64_t>)).