c++11 - C++ Supply initializer-list constructor for class template -



c++11 - C++ Supply initializer-list constructor for class template -

i have class template templ template parameter t, , templ class has info fellow member of type t, called obj. wrote variadic constructor template forwards arguments obj's constructor:

template <class t> class templ { public: template <class... args> explicit templ (args&&... args) : obj (std::forward<args>(args)...) { } private: t obj; };

now realized type t may class init-list constructor, , want accessible via templ. checked std::list::emplace , std::make_shared do. have variadic function mine, don't have overrides taking init-list. reason.

so first question: why? mean, if utilize class t init-list ctor, , utilize std::list<t>? why list::emplace not have version takes initializer_list? maybe there's reason should either... want know.

also, regardless of stl - should supply init-list ctor design? mean, it's variadic ctor, right? allowing user take type or class t utilize templ<> , straight phone call ctor defined t. if it's ctor taking init-list.

the problem forwarding initializer_list constructors trivial argument types aren't deducible (templates don't guess initializer list types):

#include <map> template<typename t> struct u { t t; template<typename...a> explicit u(a&&...a): t(std::forward<a>(a)...) {} template<typename l, typename = typename std::enable_if< std::is_constructible<t, std::initializer_list<l>>::value>::type> explicit u(std::initializer_list<l> l): t(l) {} }; u<std::map<int, int>> m{{{0, 1}, {2, 3}}}; // fails, couldn't deduce 'l'

since you'd have write m{std::initializer_list<...>{...}} in cases, there's not much point providing primitives, , not standard so.

if think interesting initializer_list arguments container types, @ approach taken in optionally supporting initializer_list construction templates maybe wrapping containers.

c++ c++11 variadic-templates initializer-list make-shared

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -