mirror of git://gcc.gnu.org/git/gcc.git
Implement LWG 2769, Redundant const in the return type of any_cast(const any&).
Implement LWG 2769, Redundant const in the return type of any_cast(const any&). * include/std/any (_AnyCast): New. (any_cast(const any&)): Use it and add an explicit cast for return. (any_cast(any&)): Likewise. (any_cast(any&&)): Likewise. * testsuite/20_util/any/misc/any_cast.cc: Add a test for a type that has an explicit copy constructor. *testsuite/20_util/any/misc/any_cast_neg.cc: Adjust. From-SVN: r243739
This commit is contained in:
parent
b7fc43d7c7
commit
6254952346
|
|
@ -1,3 +1,15 @@
|
||||||
|
2016-12-16 Ville Voutilainen <ville.voutilainen@gmail.com>
|
||||||
|
|
||||||
|
Implement LWG 2769, Redundant const in the return type of
|
||||||
|
any_cast(const any&).
|
||||||
|
* include/std/any (_AnyCast): New.
|
||||||
|
(any_cast(const any&)): Use it and add an explicit cast for return.
|
||||||
|
(any_cast(any&)): Likewise.
|
||||||
|
(any_cast(any&&)): Likewise.
|
||||||
|
* testsuite/20_util/any/misc/any_cast.cc: Add a test for a type
|
||||||
|
that has an explicit copy constructor.
|
||||||
|
* testsuite/20_util/any/misc/any_cast_neg.cc: Adjust.
|
||||||
|
|
||||||
2016-12-15 Jonathan Wakely <jwakely@redhat.com>
|
2016-12-15 Jonathan Wakely <jwakely@redhat.com>
|
||||||
|
|
||||||
PR libstdc++/59170
|
PR libstdc++/59170
|
||||||
|
|
|
||||||
|
|
@ -433,6 +433,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
|
return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename _Tp>
|
||||||
|
using _AnyCast = remove_cv_t<remove_reference_t<_Tp>>;
|
||||||
/**
|
/**
|
||||||
* @brief Access the contained object.
|
* @brief Access the contained object.
|
||||||
*
|
*
|
||||||
|
|
@ -448,9 +450,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
{
|
{
|
||||||
static_assert(any::__is_valid_cast<_ValueType>(),
|
static_assert(any::__is_valid_cast<_ValueType>(),
|
||||||
"Template argument must be a reference or CopyConstructible type");
|
"Template argument must be a reference or CopyConstructible type");
|
||||||
auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
|
auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
|
||||||
if (__p)
|
if (__p)
|
||||||
return *__p;
|
return static_cast<_ValueType>(*__p);
|
||||||
__throw_bad_any_cast();
|
__throw_bad_any_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -471,9 +473,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
{
|
{
|
||||||
static_assert(any::__is_valid_cast<_ValueType>(),
|
static_assert(any::__is_valid_cast<_ValueType>(),
|
||||||
"Template argument must be a reference or CopyConstructible type");
|
"Template argument must be a reference or CopyConstructible type");
|
||||||
auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
|
auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
|
||||||
if (__p)
|
if (__p)
|
||||||
return *__p;
|
return static_cast<_ValueType>(*__p);
|
||||||
__throw_bad_any_cast();
|
__throw_bad_any_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -485,9 +487,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
{
|
{
|
||||||
static_assert(any::__is_valid_cast<_ValueType>(),
|
static_assert(any::__is_valid_cast<_ValueType>(),
|
||||||
"Template argument must be a reference or CopyConstructible type");
|
"Template argument must be a reference or CopyConstructible type");
|
||||||
auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
|
auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
|
||||||
if (__p)
|
if (__p)
|
||||||
return *__p;
|
return static_cast<_ValueType>(*__p);
|
||||||
__throw_bad_any_cast();
|
__throw_bad_any_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -499,9 +501,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||||
{
|
{
|
||||||
static_assert(any::__is_valid_cast<_ValueType>(),
|
static_assert(any::__is_valid_cast<_ValueType>(),
|
||||||
"Template argument must be a reference or CopyConstructible type");
|
"Template argument must be a reference or CopyConstructible type");
|
||||||
auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
|
auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
|
||||||
if (__p)
|
if (__p)
|
||||||
return std::move(*__p);
|
return static_cast<_ValueType>(std::move(*__p));
|
||||||
__throw_bad_any_cast();
|
__throw_bad_any_cast();
|
||||||
}
|
}
|
||||||
// @}
|
// @}
|
||||||
|
|
|
||||||
|
|
@ -106,9 +106,22 @@ void test03()
|
||||||
MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
|
MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test04()
|
||||||
|
{
|
||||||
|
struct ExplicitCopy
|
||||||
|
{
|
||||||
|
ExplicitCopy() = default;
|
||||||
|
explicit ExplicitCopy(const ExplicitCopy&) = default;
|
||||||
|
};
|
||||||
|
any x = ExplicitCopy();
|
||||||
|
ExplicitCopy ec{any_cast<ExplicitCopy>(x)};
|
||||||
|
ExplicitCopy ec2{any_cast<ExplicitCopy>(std::move(x))};
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
test01();
|
test01();
|
||||||
test02();
|
test02();
|
||||||
test03();
|
test03();
|
||||||
|
test04();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,5 +26,5 @@ void test01()
|
||||||
using std::any_cast;
|
using std::any_cast;
|
||||||
|
|
||||||
const any y(1);
|
const any y(1);
|
||||||
any_cast<int&>(y); // { dg-error "qualifiers" "" { target { *-*-* } } 453 }
|
any_cast<int&>(y); // { dg-error "invalid static_cast" "" { target { *-*-* } } 455 }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue