Commit 52e86221 by Ville Voutilainen Committed by Ville Voutilainen

Implement std::any.

	* include/Makefile.am: Add any and c++17_warning.h to exported headers.
	* include/Makefile.in: Likewise.
	* include/std/any: New.
	* testsuite/20_util/any/assign/1.cc: Likewise.
	* testsuite/20_util/any/assign/2.cc: Likewise.
	* testsuite/20_util/any/assign/self.cc: Likewise.
	* testsuite/20_util/any/cons/1.cc: Likewise.
	* testsuite/20_util/any/cons/2.cc: Likewise.
	* testsuite/20_util/any/cons/aligned.cc: Likewise.
	* testsuite/20_util/any/cons/nontrivial.cc: Likewise.
	* testsuite/20_util/any/misc/any_cast.cc: Likewise.
	* testsuite/20_util/any/misc/any_cast_neg.cc: Likewise.
	* testsuite/20_util/any/misc/any_cast_no_rtti.cc: Likewise.
	* testsuite/20_util/any/misc/swap.cc: Likewise.
	* testsuite/20_util/any/modifiers/1.cc: Likewise.
	* testsuite/20_util/any/observers/type.cc: Likewise.
	* testsuite/20_util/any/typedefs.cc: Likewise.

From-SVN: r238061
parent 3042e708
2016-07-07 Ville Voutilainen <ville.voutilainen@gmail.com>
Implement std::any.
* include/Makefile.am: Add any and c++17_warning.h to exported headers.
* include/Makefile.in: Likewise.
* include/std/any: New.
* testsuite/20_util/any/assign/1.cc: Likewise.
* testsuite/20_util/any/assign/2.cc: Likewise.
* testsuite/20_util/any/assign/self.cc: Likewise.
* testsuite/20_util/any/cons/1.cc: Likewise.
* testsuite/20_util/any/cons/2.cc: Likewise.
* testsuite/20_util/any/cons/aligned.cc: Likewise.
* testsuite/20_util/any/cons/nontrivial.cc: Likewise.
* testsuite/20_util/any/misc/any_cast.cc: Likewise.
* testsuite/20_util/any/misc/any_cast_neg.cc: Likewise.
* testsuite/20_util/any/misc/any_cast_no_rtti.cc: Likewise.
* testsuite/20_util/any/misc/swap.cc: Likewise.
* testsuite/20_util/any/modifiers/1.cc: Likewise.
* testsuite/20_util/any/observers/type.cc: Likewise.
* testsuite/20_util/any/typedefs.cc: Likewise.
2016-07-06 Ville Voutilainen <ville.voutilainen@gmail.com>
Add a new header for diagnosing the use of C++17 facilities
......
......@@ -27,6 +27,7 @@ std_srcdir = ${glibcxx_srcdir}/include/std
std_builddir = .
std_headers = \
${std_srcdir}/algorithm \
${std_srcdir}/any \
${std_srcdir}/array \
${std_srcdir}/atomic \
${std_srcdir}/bitset \
......@@ -92,6 +93,7 @@ bits_headers = \
${bits_srcdir}/boost_concept_check.h \
${bits_srcdir}/c++0x_warning.h \
${bits_srcdir}/c++14_warning.h \
${bits_srcdir}/c++17_warning.h \
${bits_srcdir}/char_traits.h \
${bits_srcdir}/codecvt.h \
${bits_srcdir}/concept_check.h \
......
......@@ -317,6 +317,7 @@ std_srcdir = ${glibcxx_srcdir}/include/std
std_builddir = .
std_headers = \
${std_srcdir}/algorithm \
${std_srcdir}/any \
${std_srcdir}/array \
${std_srcdir}/atomic \
${std_srcdir}/bitset \
......@@ -382,6 +383,7 @@ bits_headers = \
${bits_srcdir}/boost_concept_check.h \
${bits_srcdir}/c++0x_warning.h \
${bits_srcdir}/c++14_warning.h \
${bits_srcdir}/c++17_warning.h \
${bits_srcdir}/char_traits.h \
${bits_srcdir}/codecvt.h \
${bits_srcdir}/concept_check.h \
......
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
void test01()
{
any x;
any y;
y = x;
VERIFY( x.empty() );
VERIFY( y.empty() );
y = std::move(x);
VERIFY( x.empty() );
VERIFY( y.empty() );
}
void test02()
{
any x(1);
any y;
y = x;
VERIFY( !x.empty() );
VERIFY( !y.empty() );
x = std::move(y);
VERIFY( !x.empty() );
VERIFY( y.empty() );
x = y;
VERIFY( x.empty() );
VERIFY( y.empty() );
}
int main()
{
test01();
test02();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
using std::any_cast;
struct X
{
bool moved = false;
bool moved_from = false;
X() = default;
X(const X&) = default;
X(X&& x) : moved(true) { x.moved_from = true; }
};
void test01()
{
X x;
any a1;
a1 = x;
VERIFY(x.moved_from == false);
any a2;
a2 = std::move(x);
VERIFY(x.moved_from == true);
VERIFY(any_cast<X&>(a2).moved == true );
}
int main()
{
test01();
}
// Copyright (C) 2015-2016 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
#include <any>
#include <set>
#include <testsuite_hooks.h>
std::set<const void*> live_objects;
struct A {
A() { live_objects.insert(this); }
~A() { live_objects.erase(this); }
A(const A& a) { VERIFY(live_objects.count(&a)); live_objects.insert(this); }
};
void
test01()
{
using std::any;
any a;
a = a;
VERIFY( a.empty() );
a = A{};
a = a;
VERIFY( !a.empty() );
a.clear();
VERIFY( live_objects.empty() );
}
void
test02()
{
using std::any;
struct X {
any a;
};
X x;
std::swap(x, x); // results in "self-move-assignment" of X::a
VERIFY( x.a.empty() );
x.a = A{};
std::swap(x, x); // results in "self-move-assignment" of X::a
VERIFY( !x.a.empty() );
x.a.clear();
VERIFY( live_objects.empty() );
}
void
test03()
{
using std::any;
any a;
a.swap(a);
VERIFY( a.empty() );
a = A{};
a.swap(a);
VERIFY( !a.empty() );
a.clear();
VERIFY( live_objects.empty() );
}
int
main()
{
test01();
test02();
test03();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
void test01()
{
any x;
VERIFY( x.empty() );
any y(x);
VERIFY( x.empty() );
VERIFY( y.empty() );
any z(std::move(y));
VERIFY( y.empty() );
VERIFY( z.empty() );
}
void test02()
{
any x(1);
VERIFY( !x.empty() );
any y(x);
VERIFY( !x.empty() );
VERIFY( !y.empty() );
any z(std::move(y));
VERIFY( y.empty() );
VERIFY( !z.empty() );
}
int main()
{
test01();
test02();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
using std::any_cast;
struct X
{
bool moved = false;
bool moved_from = false;
X() = default;
X(const X&) = default;
X(X&& x) : moved(true) { x.moved_from = true; }
};
void test01()
{
X x;
any a1(x);
VERIFY(x.moved_from == false);
any a2(std::move(x));
VERIFY(x.moved_from == true);
VERIFY(any_cast<X&>(a2).moved == true );
}
int main()
{
test01();
}
// Copyright (C) 2015-2016 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
#include <any>
#include <cstdint>
#include <testsuite_hooks.h>
// Alignment requiremnts of this type prevent it being stored in 'any'
struct alignas(2 * alignof(void*)) X { };
bool
stored_internally(void* obj, const std::any& a)
{
std::uintptr_t a_addr = reinterpret_cast<std::uintptr_t>(&a);
std::uintptr_t a_end = a_addr + sizeof(a);
std::uintptr_t obj_addr = reinterpret_cast<std::uintptr_t>(obj);
return (a_addr <= obj_addr) && (obj_addr < a_end);
}
void
test01()
{
std::any a = X{};
X& x = std::any_cast<X&>(a);
VERIFY( !stored_internally(&x, a) );
a = 'X';
char& c = std::any_cast<char&>(a);
VERIFY( stored_internally(&c, a) );
}
int
main()
{
test01();
}
// Copyright (C) 2015-2016 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++17" }
#include <any>
#include <testsuite_hooks.h>
struct LocationAware
{
LocationAware() { }
~LocationAware() { VERIFY(self == this); }
LocationAware(const LocationAware&) { }
LocationAware& operator=(const LocationAware&) { return *this; }
LocationAware(LocationAware&&) noexcept { }
LocationAware& operator=(LocationAware&&) noexcept { return *this; }
void* const self = this;
};
static_assert(std::is_nothrow_move_constructible<LocationAware>::value, "");
static_assert(!std::is_trivially_copyable<LocationAware>::value, "");
using std::any;
void
test01()
{
LocationAware l;
any a = l;
}
void
test02()
{
LocationAware l;
any a = l;
any b = a;
{
any tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
}
void
test03()
{
LocationAware l;
any a = l;
any b = a;
swap(a, b);
}
int
main()
{
test01();
test02();
test03();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <string>
#include <cstring>
#include <testsuite_hooks.h>
using std::any;
using std::any_cast;
void test01()
{
using std::string;
using std::strcmp;
// taken from example in N3804 proposal
any x(5); // x holds int
VERIFY(any_cast<int>(x) == 5); // cast to value
any_cast<int&>(x) = 10; // cast to reference
VERIFY(any_cast<int>(x) == 10);
x = "Meow"; // x holds const char*
VERIFY(strcmp(any_cast<const char*>(x), "Meow") == 0);
any_cast<const char*&>(x) = "Harry";
VERIFY(strcmp(any_cast<const char*>(x), "Harry") == 0);
x = string("Meow"); // x holds string
string s, s2("Jane");
s = move(any_cast<string&>(x)); // move from any
VERIFY(s == "Meow");
any_cast<string&>(x) = move(s2); // move to any
VERIFY(any_cast<const string&>(x) == "Jane");
string cat("Meow");
const any y(cat); // const y holds string
VERIFY(any_cast<const string&>(y) == cat);
}
void test02()
{
using std::bad_any_cast;
any x(1);
auto p = any_cast<double>(&x);
VERIFY(p == nullptr);
x = 1.0;
p = any_cast<double>(&x);
VERIFY(p != nullptr);
x = any();
p = any_cast<double>(&x);
VERIFY(p == nullptr);
try {
any_cast<double>(x);
VERIFY(false);
} catch (const bad_any_cast&) {
}
}
static int move_count = 0;
void test03()
{
struct MoveEnabled
{
MoveEnabled(MoveEnabled&&)
{
++move_count;
}
MoveEnabled() = default;
MoveEnabled(const MoveEnabled&) = default;
};
MoveEnabled m;
MoveEnabled m2 = any_cast<MoveEnabled>(any(m));
VERIFY(move_count == 1);
MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m));
VERIFY(move_count == 1);
struct MoveDeleted
{
MoveDeleted(MoveDeleted&&) = delete;
MoveDeleted() = default;
MoveDeleted(const MoveDeleted&) = default;
};
MoveDeleted md;
MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md)));
MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
}
int main()
{
test01();
test02();
test03();
}
// { dg-options "-std=gnu++17" }
// { dg-do compile }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
void test01()
{
using std::any;
using std::any_cast;
const any y(1);
any_cast<int&>(y); // { dg-error "qualifiers" "" { target { *-*-* } } 357 }
}
// { dg-options "-std=gnu++17 -fno-rtti" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <string>
#include <cstring>
#include <testsuite_hooks.h>
using std::any;
using std::any_cast;
void test01()
{
using std::bad_any_cast;
any x(1);
auto p = any_cast<double>(&x);
VERIFY(p == nullptr);
x = 1.0;
p = any_cast<double>(&x);
VERIFY(p != nullptr);
x = any();
p = any_cast<double>(&x);
VERIFY(p == nullptr);
try {
any_cast<double>(x);
VERIFY(false);
} catch (const bad_any_cast&) {
}
}
int main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
void test01()
{
any x(1);
any y;
swap(x, y);
VERIFY( x.empty() );
VERIFY( !y.empty() );
}
int main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
void test01()
{
any x(1);
any y;
x.swap(y);
VERIFY( x.empty() );
VERIFY( !y.empty() );
x.swap(y);
VERIFY( !x.empty() );
VERIFY( y.empty() );
x.clear();
VERIFY( x.empty() );
}
int main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do run }
// Copyright (C) 2014-2016 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
// <http://www.gnu.org/licenses/>.
#include <any>
#include <testsuite_hooks.h>
using std::any;
void test01()
{
any x;
VERIFY( x.type() == typeid(void) );
x = 1;
VERIFY( x.type() == typeid(int) );
x = any();
VERIFY( x.type() == typeid(void) );
}
int main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do compile }
// Copyright (C) 2014-2016 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 moved_to of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <any>
#include <type_traits>
#include <typeinfo>
using check1_t = std::any;
using check2_t = std::bad_any_cast;
static_assert(std::is_base_of<std::bad_cast, check2_t>::value,
"bad_any_cast must derive from bad_cast");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment