From 14b846de5134c67084100ace478e353e93745f83 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 8 Nov 2010 23:42:09 +0000 Subject: [PATCH] unique_ptr.h: Move misplaced static_assert and use tuple's constexpr constructor in constexpr... 2010-11-08 Jonathan Wakely * include/bits/unique_ptr.h: Move misplaced static_assert and use tuple's constexpr constructor in constexpr constructors. * testsuite/20_util/unique_ptr/cons/ptr_deleter.cc: New. * testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc: New. From-SVN: r166460 --- libstdc++-v3/include/bits/unique_ptr.h | 15 ++-- .../20_util/unique_ptr/cons/ptr_deleter.cc | 68 +++++++++++++++++++ .../unique_ptr/cons/ptr_deleter_neg.cc | 57 ++++++++++++++++ 3 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter.cc create mode 100644 libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 4dc4dddb28e..ff1a9251dbc 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -106,9 +106,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std) typedef _Tp element_type; typedef _Dp deleter_type; - static_assert(!std::is_pointer::value, - "constructed with null function pointer deleter"); - // Constructors. constexpr unique_ptr() : _M_t() @@ -132,7 +129,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) "rvalue deleter bound to reference"); } constexpr unique_ptr(nullptr_t) - : _M_t(pointer(), deleter_type()) + : _M_t() { } // Move constructors. @@ -269,18 +266,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std) typedef _Tp element_type; typedef _Dp deleter_type; - static_assert(!std::is_pointer::value, - "constructed with null function pointer deleter"); - // Constructors. constexpr unique_ptr() - : _M_t(pointer(), deleter_type()) + : _M_t() { } explicit unique_ptr(pointer __p) : _M_t(__p, deleter_type()) - { } + { static_assert(!std::is_pointer::value, + "constructed with null function pointer deleter"); } unique_ptr(pointer __p, typename std::conditional::value, @@ -295,7 +290,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) /* TODO: use delegating constructor */ constexpr unique_ptr(nullptr_t) - : _M_t(pointer(), deleter_type()) + : _M_t() { } // Move constructors. diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter.cc new file mode 100644 index 00000000000..7e88eb94804 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter.cc @@ -0,0 +1,68 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do run } + +// Copyright (C) 2010 Free Software Foundation +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 20.9.10 Template class unique_ptr [unique.ptr] + +#include +#include + +static int count; + +void del(int* p) { ++count; delete p; } +void vdel(int* p) { ++count; delete[] p; } + +void +test01() +{ + bool test __attribute__((unused)) = true; + count = 0; + { + std::unique_ptr p(nullptr, del); + } + VERIFY( count == 0 ); + { + std::unique_ptr p(new int, del); + } + VERIFY( count == 1 ); +} + +void +test02() +{ + bool test __attribute__((unused)) = true; + count = 0; + { + std::unique_ptr p(nullptr, vdel); + } + VERIFY( count == 0 ); + { + std::unique_ptr p(new int[1], vdel); + } + VERIFY( count == 1 ); +} + +int +main() +{ + test01(); + test02(); + return 0; +} + diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc new file mode 100644 index 00000000000..143578ed4b4 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/ptr_deleter_neg.cc @@ -0,0 +1,57 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } + +// Copyright (C) 2010 Free Software Foundation +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 20.6.11 Template class unique_ptr [unique.ptr] + +#include +#include + +using std::unique_ptr; + +// { dg-excess-errors "static assertion failed" } + +void +test01() +{ + unique_ptr p1; // { dg-error "here" "" { xfail *-*-* } } + + unique_ptr p2(nullptr); // { dg-error "here" "" { xfail *-*-* } } + + unique_ptr p3(new int); // { dg-error "here" } +} + +void +test02() +{ + unique_ptr p1; // { dg-error "here" "" { xfail *-*-* } } + + unique_ptr p2(nullptr); // { dg-error "here" "" { xfail *-*-* } } + + unique_ptr p3(new int[1]); // { dg-error "here" } +} + + +int +main() +{ + test01(); + test02(); + return 0; +}