c++ - initializer list and conversion to everything, construction order -
situation like:
#include <utility> #include <typeinfo> #include <iostream> struct c1 { char const* str; template <typename t> operator t() const { std::cout << "convert " << typeid(t).name() << "\n"; return {}; } }; struct c2 { c2(c1 const&) { std::cout << "c2(c1)\n"; } c2(std::initializer_list<std::pair<char const*, int>>) { std::cout << "c2(list)\n"; } }; int main() { c1 c1{}; c2 c2{c1}; } output indicates c2(list) being called.
i c2(c1) called c1 argument, need parameters of std::initializer list remain deducible , convertible, , can not replace variadic-templated version. want control construction order, here //2 not template. suppose type std::pair can deserialized in normal conditions. c++14 may used
the following selects c2(c1) style both style, {...} , ({...}) (in strict c++11 standards, ({...}) style, select second, because c1 aggregate , special rules applied it. no longer case more recent standards.). works making second constructor no longer initializer constructor.
template<typename t> struct id { typedef t type; }; struct c1 { char const* str; template <typename t> operator t() const { std::cout << "convert " << typeid(t).name() << "\n"; return {}; } }; struct c2 { c2(c1); template<typename t = std::initializer_list<std::pair<char const*, int>>> c2(typename id<t>::type); }; int main() { c1 c1{}; c2 c2{c1}; c2 c21({c1}); }
Comments
Post a Comment