From patchwork Thu Jul 10 18:08:36 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 368736 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E466914008F for ; Fri, 11 Jul 2014 04:09:05 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=JKhkKccc60Sw1aaf9HVRWivGA+EMe86+yjVDYhdkhq5TthP8WY5ua gnwrVz89hzDB1/BvXhlcby7X12HoNWSmxYqbVpbROWxmvdO6AYEfSZGH7HRKb1rZ xifm3KYCM/WGwwtu1aZt4pmnbchSXS/k96YbMd4CmRWPCqAK4JC20Q= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=3VaZyC5209PN4UCmW7Z56hBx7LM=; b=Yyj+GLogT3l2sHbsP5+t cwMX+6quyxsbadqCFD7UBMKq+PIJ5Qk/rwrm84STveB2NbytjTvR4gpxNL/cfhpG PJQSOBwN7ritQ2K4pCfHZtbIFaavhbtBrGqoqWY7svkYNJRN60BAFUx13AM6xqce CbwQQpX3wPcDsUWAz9H8SSc= Received: (qmail 5121 invoked by alias); 10 Jul 2014 18:08:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 5074 invoked by uid 89); 10 Jul 2014 18:08:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 10 Jul 2014 18:08:39 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s6AI8cGI027996 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Jul 2014 14:08:38 -0400 Received: from localhost (vpn1-7-180.ams2.redhat.com [10.36.7.180]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s6AI8bkn022042; Thu, 10 Jul 2014 14:08:37 -0400 Date: Thu, 10 Jul 2014 19:08:36 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] Fix bug in experimental::any Message-ID: <20140710180836.GA4871@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This fixes the uses-allocator construction in experimental::any and re-orders the data members so that I can extract the stored value in a Python printer without having to walk the inheritance hierarchy of a std::tuple. The actual pretty printer for any (and string_view and optional) will follow Real Soon Now. Tested x86_64-linux, committed to trunk. commit a5b7242e6eed8f4bbac02ddfb9c798d55481e264 Author: Jonathan Wakely Date: Thu Jul 10 15:22:35 2014 +0100 * include/experimental/any (any::_Manager_alloc::_Data): Reorder tuple members to simplify pretty printing. (any::_Manager_alloc::_Data::_M_construct): Fix uses-allocator construction. * testsuite/experimental/any/cons/4.cc: New. diff --git a/libstdc++-v3/include/experimental/any b/libstdc++-v3/include/experimental/any index a69d006..a4ac983 100644 --- a/libstdc++-v3/include/experimental/any +++ b/libstdc++-v3/include/experimental/any @@ -450,18 +450,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _Traits = std::allocator_traits<_Alloc>; - std::tuple<_Alloc, __gnu_cxx::__aligned_buffer<_Tp>> _M_data; + std::tuple<__gnu_cxx::__aligned_buffer<_Tp>, _Alloc> _M_data; - _Alloc& _M_alloc() { return std::get<0>(_M_data); } - const _Alloc& _M_alloc() const { return std::get<0>(_M_data); } + _Alloc& _M_alloc() { return std::get<1>(_M_data); } + const _Alloc& _M_alloc() const { return std::get<1>(_M_data); } - _Tp* _M_obj() { return std::get<1>(_M_data)._M_ptr(); } - const _Tp* _M_obj() const { return std::get<1>(_M_data)._M_ptr(); } + _Tp* _M_obj() { return std::get<0>(_M_data)._M_ptr(); } + const _Tp* _M_obj() const { return std::get<0>(_M_data)._M_ptr(); } template - _Data(const _Alloc& __a, _Up&& __val) : _M_data(__a, nullptr) + _Data(const _Alloc& __a, _Up&& __val) : _M_data(nullptr, __a) { - this->_M_construct(std::__use_alloc<_Tp>(_M_alloc()), + this->_M_construct(std::__use_alloc<_Tp, _Alloc, _Up&&>(_M_alloc()), std::forward<_Up>(__val)); } @@ -479,8 +479,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_construct(__uses_alloc1<_Alloc> __a, _Up&& __val) { - _Traits::construct(__a._M_a, _M_obj(), - std::allocator_arg, __a._M_a, + _Traits::construct(_M_alloc(), _M_obj(), + std::allocator_arg, *__a._M_a, std::forward<_Up>(__val)); } @@ -488,8 +488,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_construct(__uses_alloc2<_Alloc> __a, _Up&& __val) { - _Traits::construct(__a._M_a, _M_obj(), - std::forward<_Up>(__val), __a._M_a); + _Traits::construct(_M_alloc(), _M_obj(), + std::forward<_Up>(__val), *__a._M_a); } }; diff --git a/libstdc++-v3/testsuite/experimental/any/cons/4.cc b/libstdc++-v3/testsuite/experimental/any/cons/4.cc new file mode 100644 index 0000000..6e5e019 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/any/cons/4.cc @@ -0,0 +1,73 @@ +// { dg-options "-std=gnu++14" } +// { dg-do run } + +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// 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 +// . + +#include +#include +#include + +using std::experimental::any; + +struct NotSmall +{ + char c[64]; // prevent small-object optimization +}; + +struct T1 +{ + using allocator_type = std::allocator; + + T1() = default; + T1(const T1&) : used_alloc(false) { } + T1(const T1&, const allocator_type&) : used_alloc(true) { } + + bool used_alloc; + + NotSmall x; +}; + +struct T2 +{ + using allocator_type = std::allocator; + + T2() = default; + T2(const T2&) : used_alloc(false) { } + T2(std::allocator_arg_t, const allocator_type&, const T2&) : used_alloc(true) + { } + + bool used_alloc; + + NotSmall x; +}; + +bool test [[gnu::unused]] = true; + +void test01() +{ + any x1(std::allocator_arg, std::allocator{}, T1{}); + VERIFY( std::experimental::any_cast(x1).used_alloc ); + + any x2(std::allocator_arg, std::allocator{}, T2{}); + VERIFY( std::experimental::any_cast(x2).used_alloc ); +} + +int main() +{ + test01(); +}