Skip to content

Commit e8374f7

Browse files
committed
more tests and better diagnostics for unique_any (refs #24)
1 parent a090bf3 commit e8374f7

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

include/boost/any/fwd.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ namespace detail {
3333

3434
template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
3535
struct is_basic_any<boost::anys::basic_any<OptimizeForSize, OptimizeForAlignment> > : public true_type {};
36+
37+
template <class T>
38+
struct is_some_any: public is_basic_any<T> {};
39+
40+
template <>
41+
struct is_some_any<boost::any>: public boost::true_type {};
42+
43+
template <>
44+
struct is_some_any<boost::anys::unique_any>: public boost::true_type {};
45+
3646
} // namespace detail
3747

3848
} // namespace anys

include/boost/any/unique_any.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <utility>
3535
#include <type_traits>
3636

37+
#include <boost/any/fwd.hpp>
3738
#include <boost/any/bad_any_cast.hpp>
3839

3940
#include <boost/type_index.hpp>
@@ -58,13 +59,19 @@ class unique_any {
5859

5960
// Perfect forwarding of T
6061
template<typename T>
61-
unique_any(T&& value
62-
, typename std::enable_if<
63-
!std::is_same<unique_any&, T>::value // disable if value has type `unique_any&`
64-
&& !std::is_const<T>::value
65-
>::type* = 0) // disable if value has type `const T&&`
62+
unique_any(T&& value)
6663
: content(new holder< typename std::decay<T>::type >(std::forward<T>(value)))
6764
{
65+
static_assert(
66+
!boost::anys::detail::is_basic_any< typename std::decay<T>::type >::value,
67+
"boost::anys::unique_any could not be constructed from boost::anys::basic_any."
68+
);
69+
70+
static_assert(
71+
!boost::anys::detail::is_some_any< typename std::decay<T>::type >::value,
72+
"unique_any could be only moved and could not be constructoed from "
73+
"other types of any."
74+
);
6875
}
6976

7077
template<class T, class... Args>

test/unique_any/Jamfile.v2

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# Copyright Vladimir Prus 2005. Use, modification and
1+
# Copyright Vladimir Prus 2005.
2+
# Copyright Antony Polukhin, 2013-2023.
3+
#
4+
# Use, modification and
25
# distribution is subject to the Boost Software License, Version
36
# 1.0. (See accompanying file LICENSE_1_0.txt or copy at
47
# http://www.boost.org/LICENSE_1_0.txt)
58
#
69
# For more information, see http://www.boost.org/libs/any
7-
#
10+
811
import ../../config/checks/config : requires ;
912

1013
import testing ;
@@ -16,15 +19,19 @@ project
1619
;
1720

1821
test-suite unique_any :
19-
[ run base.cpp : : : : base ]
20-
[ run base.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_base ]
21-
[ run move.cpp : : : : move ]
22-
[ run move.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_move ]
23-
[ run emplace.cpp : : : : emplace ]
24-
[ run emplace.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_emplace ]
25-
[ compile-fail any_cast_cv_failed.cpp ]
26-
[ compile-fail temporary_to_ref_failed.cpp ]
27-
[ compile-fail cv_to_rv_failed.cpp ]
22+
[ run base.cpp : : : : unique_base ]
23+
[ run base.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_unique_base ]
24+
[ run move.cpp : : : : unique_move ]
25+
[ run move.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_unique_move ]
26+
[ run emplace.cpp : : : : unique_emplace ]
27+
[ run emplace.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : no_rtti_unique_emplace ]
28+
[ compile-fail any_cast_cv_failed.cpp : : unique_any_cast_cd_failed ]
29+
[ compile-fail temporary_to_ref_failed.cpp : : unique_temporary_to_ref_failed ]
30+
[ compile-fail cv_to_rv_failed.cpp : : unique_cv_to_rv_failed ]
31+
[ compile-fail const_rvalue_construction_failed.cpp : : unique_const_rvalue_construction_failed ]
32+
33+
[ compile-fail from_any_failed.cpp : : unique_from_any_failed ]
34+
[ compile-fail from_basic_any_failed.cpp : : unique_from_basic_any_failed ]
2835
;
2936

3037

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Antony Polukhin, 2013-2023.
2+
//
3+
// Distributed under the Boost
4+
// Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
6+
7+
#include <boost/any/unique_any.hpp>
8+
9+
const boost::anys::unique_any&& get() {
10+
static const boost::anys::unique_any a;
11+
return std::move(a);
12+
}
13+
14+
int main() {
15+
boost::anys::unique_any b(get());
16+
(void)b;
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Antony Polukhin, 2013-2023.
2+
//
3+
// Distributed under the Boost
4+
// Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
6+
7+
#include <boost/any.hpp>
8+
#include <boost/any/unique_any.hpp>
9+
10+
int main() {
11+
boost::any a;
12+
boost::anys::unique_any b(a);
13+
(void)b;
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Antony Polukhin, 2013-2023.
2+
//
3+
// Distributed under the Boost
4+
// Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
6+
7+
#include <boost/any/basic_any.hpp>
8+
#include <boost/any/unique_any.hpp>
9+
10+
int main() {
11+
boost::anys::basic_any<> a;
12+
boost::anys::unique_any b(a);
13+
(void)b;
14+
}

0 commit comments

Comments
 (0)