我把这个问题简化了一些:
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;
} …Run Code Online (Sandbox Code Playgroud) c++ templates casting template-meta-programming type-deduction