From: Yedidya Feldblum Date: Mon, 23 May 2016 06:35:19 +0000 (-0700) Subject: Avoid std::initializer_list ctor in folly::dynamic::array X-Git-Tag: 2016.07.26~210 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b939def1c52c5d759a984483b52a54fdc5c59e97;p=folly.git Avoid std::initializer_list ctor in folly::dynamic::array Summary: [Folly] Avoid `std::initializer_list` ctor in `folly::dynamic::array`. This will help deprecate the dangerous initializer-list constructor. The init-list construction is dangerous because its semantics vary by compiler and by compiler version. Reviewed By: ot Differential Revision: D3333148 fbshipit-source-id: 031e7593b7e28b299d5eeeb5ce7aed9216dd3a5c --- diff --git a/folly/dynamic-inl.h b/folly/dynamic-inl.h index 91057eea..920f627c 100644 --- a/folly/dynamic-inl.h +++ b/folly/dynamic-inl.h @@ -209,8 +209,7 @@ inline void dynamic::array(EmptyArrayTag) {} template inline dynamic dynamic::array(Args&& ...args) { - return dynamic(std::initializer_list{std::forward(args)...}, - PrivateTag()); + return dynamic(Array{std::forward(args)...}, PrivateTag()); } // This looks like a case for perfect forwarding, but our use of @@ -308,17 +307,10 @@ inline dynamic::dynamic(std::string&& s) : type_(STRING) { } inline dynamic::dynamic(std::initializer_list il) - : dynamic(std::move(il), PrivateTag()) { -} - -inline dynamic::dynamic(std::initializer_list il, PrivateTag) - : type_(ARRAY) -{ - new (&u_.array) Array(il.begin(), il.end()); -} + : dynamic(Array(std::move(il)), PrivateTag()) {} inline dynamic& dynamic::operator=(std::initializer_list il) { - (*this) = dynamic(il, PrivateTag()); + (*this) = dynamic(Array(std::move(il)), PrivateTag()); return *this; } @@ -668,6 +660,10 @@ inline void dynamic::pop_back() { ////////////////////////////////////////////////////////////////////// +inline dynamic::dynamic(Array&& r, PrivateTag) : type_(ARRAY) { + new (&u_.array) Array(std::move(r)); +} + #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \ template <> struct dynamic::TypeInfo { \ static constexpr const char* name = str; \ diff --git a/folly/dynamic.h b/folly/dynamic.h index ea407b30..cc75cdfa 100644 --- a/folly/dynamic.h +++ b/folly/dynamic.h @@ -179,7 +179,6 @@ public: FOLLY_DEPRECATED( "Initializer list syntax is deprecated (#10300209). Use dynamic::array.") /* implicit */ dynamic(std::initializer_list il); - dynamic(std::initializer_list il, PrivateTag); FOLLY_DEPRECATED( "Initializer list syntax is deprecated (#10300209). Use dynamic::array.") dynamic& operator=(std::initializer_list il); @@ -536,6 +535,8 @@ private: template struct GetAddrImpl; template struct PrintImpl; + dynamic(Array&& array, PrivateTag); + template T const& get() const; template T& get(); template T* get_nothrow() & noexcept;