From: Douglas Gregor Date: Sat, 18 Oct 2008 23:02:17 +0000 (+0000) Subject: stl_pair.h (__may_be_null_pointer_init): New. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c393ec5a87f11010629763aa1cc34e1803642633;p=gcc.git stl_pair.h (__may_be_null_pointer_init): New. 2008-10-18 Douglas Gregor * include/bits/stl_pair.h (__may_be_null_pointer_init): New. (pair::pair): Eliminate the redundant pair(U1&&, U2&&) constructor. Add lvalue pair constructor to handle non-const pair lvalues. Remove the old variadic constructor, and instead provide several variadic constructors that avoid failing when attempting to initialize a pointer from a null pointer constant. * testsuite/20_util/pair/moveable.cc (test3): Add new tests with initialization of pointers from the null pointer constant. From-SVN: r141214 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 18aa1778d5e..cbbc0efe652 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2008-10-18 Douglas Gregor + + * include/bits/stl_pair.h (__may_be_null_pointer_init): New. + (pair::pair): Eliminate the redundant pair(U1&&, U2&&) constructor. + Add lvalue pair constructor to handle non-const pair lvalues. + Remove the old variadic constructor, and instead provide several + variadic constructors that avoid failing when attempting to + initialize a pointer from a null pointer constant. + * testsuite/20_util/pair/moveable.cc (test3): Add new tests with + initialization of pointers from the null pointer constant. + 2008-10-17 Paolo Carlini * include/bits/forward_list.tcc (forward_list<>:: diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index cf61b09945a..c169275ece3 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -65,8 +65,35 @@ #include // for std::move / std::forward, std::decay, and // std::swap +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +#include +#endif + _GLIBCXX_BEGIN_NAMESPACE(std) +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +// A trait that determines whether the initialization of a T from +// arguments of type Args could possibly be the initialization of a +// pointer from a null pointer constant. +template +struct __may_be_null_pointer_init + : public false_type { }; + +template +struct __may_be_null_pointer_init<_Tp*, _Up> + : public integral_constant::type>::value + || is_enum::type>::value)> + { }; + +template +struct __may_be_null_pointer_init<_Tp _Class::*, _Up> + : public integral_constant::type>::value + || is_enum::type>::value)> + { }; +#endif + /// pair holds two objects of arbitrary type. template struct pair @@ -89,10 +116,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) : first(__a), second(__b) { } #ifdef __GXX_EXPERIMENTAL_CXX0X__ - template - pair(_U1&& __x, _U2&& __y) - : first(std::forward<_U1>(__x)), - second(std::forward<_U2>(__y)) { } + // Omitted the following constructor, which appears in the C++0x + // working paper but is redundant with the variadic constructors + // below. + // + // template + // pair(_U1&& __x, _U2&& __y); pair(pair&& __p) : first(std::move(__p.first)), @@ -111,12 +140,56 @@ _GLIBCXX_BEGIN_NAMESPACE(std) : first(std::move(__p.first)), second(std::move(__p.second)) { } - // http://gcc.gnu.org/ml/libstdc++/2007-08/msg00052.html - template - pair(_U1&& __x, _Arg0&& __arg0, _Args&&... __args) + // This constructor is required so that lvalue pairs don't get + // pushed to the variadic constructor below. + template + pair(pair<_U1, _U2>& __p) + : first(const_cast&>(__p).first), + second(const_cast&>(__p).second) { } + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 811. pair of pointers no longer works with literal 0 + + // Variadic constructor. The enable_if makes sure that we won't + // try to initialize a pointer from an integral type, which + // needs to be handled by a different constructor that will + // convert a null pointer constant to that pointer type. See + // library issue 767. + template::value + && !__may_be_null_pointer_init<_T2, _Args...>::value), + void>::type> + pair(_U1&& __x, _Args&&... __args) + : first(std::forward<_U1>(__x)), + second(std::forward<_Args>(__args)...) { } + + // Variadic constructor. The enable_if makes sure that the + // second argument isn't going to try to initialize a pointer + // from an integral type. However, T1 may be a pointer whose + // argument was a null pointer constant. + template::value, + void>::type> + pair(const _T1& __x, _Args&&... __args) + : first(__x), + second(std::forward<_Args>(__args)...) { } + + // Constructor typically used when T2 is a pointer and the + // second argument was a null pointer constant. The enable_if + // makes sure that the first argument isn't going to try to + // initialize a pointer from an integral type. + template::value, + void>::type> + pair(_U1&& __x, const _T2& __y) : first(std::forward<_U1>(__x)), - second(std::forward<_Arg0>(__arg0), - std::forward<_Args>(__args)...) { } + second(__y) { } pair& operator=(pair&& __p) diff --git a/libstdc++-v3/testsuite/20_util/pair/moveable.cc b/libstdc++-v3/testsuite/20_util/pair/moveable.cc index cd5de4162eb..4e5c53583cd 100644 --- a/libstdc++-v3/testsuite/20_util/pair/moveable.cc +++ b/libstdc++-v3/testsuite/20_util/pair/moveable.cc @@ -64,9 +64,54 @@ test2() r.second.size() == 2 && p.second.size() == 0); } +struct X { + explicit X(int, int) { } + +private: + X(const X&) = delete; +}; + +struct move_only { + move_only() { } + move_only(move_only&&) { } + +private: + move_only(const move_only&) = delete; +}; + +void +test3() +{ + int *ip = 0; + int X::*mp = 0; + std::pair p1(0, 0); + std::pair p2(ip, 0); + std::pair p3(0, ip); + std::pair p4(ip, ip); + + std::pair p5(0, 0); + std::pair p6(mp, 0); + std::pair p7(0, mp); + std::pair p8(mp, mp); + + std::pair p9(0, 1, 2); + std::pair p10(0, 1, 2); + std::pair p11(ip, 1, 2); + std::pair p12(mp, 1, 2); + + std::pair p13(0); + std::pair p14(0); + + std::pair p15(0, move_only()); + std::pair p16(0, move_only()); + std::pair p17(move_only(), 0); + std::pair p18(move_only(), 0); +} + int main() { test1(); test2(); + test3(); }