I have simplified my somewhat more difficult problem to this:
http://coliru.stacked-crooked.com/a/2660b33492651e92
#include <iostream>
#include <string>
#include <type_traits>
template<typename C>
struct get_type
{
C operator()() const = delete;
};
template<>
struct get_type<std::string>
{
std::string operator()() const { return "asd"; }
};
template<>
struct get_type<size_t> {
size_t operator()() const { return 6; }
};
struct S
{
S(){}
template<typename T>
operator T() { return get_type<T>{}(); }
};
struct A
{
A() :s{S{}}, n{S{}} {}
std::string s;
size_t n;
};
int main()
{
A a;
std::cout << "Spock out." << std::endl;
}
This generates the following error:
'In instantiation of 'S::operator T() [with T = char]':'...
Why is T is deduced to char and not std::string?
Edit:
@YSC's answer seems to be correct: https://mcmap.net/q/1305935/-c-template-type-deduction-fail-in-cast-operator
I edited the post to add a solution: http://coliru.stacked-crooked.com/a/06d31d981acd2544
struct S
{
S(){}
template<typename T>
explicit operator T() { return get_type<T>{}(); }
};
return get_type<T>{}();
withreturn {};
, GCC actually compiles the code. I'm guessing GCC makes the list constructor greedy in this context and Clang doesn't. – Bartolemo