diff options
author | Pedro Alves <palves@redhat.com> | 2017-04-18 21:39:24 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-04-18 23:48:41 +0100 |
commit | 22796e972f18c5601cecb0251222411a352836b6 (patch) | |
tree | 0e2cdd295c54b35894588485dda6d821648540d7 /gdb/common/traits.h | |
parent | a7fc9b6193f830293066479aa439cea924418393 (diff) | |
download | gdb-22796e972f18c5601cecb0251222411a352836b6.zip gdb-22796e972f18c5601cecb0251222411a352836b6.tar.gz gdb-22796e972f18c5601cecb0251222411a352836b6.tar.bz2 |
More gdb::optional features
Currently we can't use gdb::optional<T> as function return type,
because gdb::optional's copy ctor is deleted. For example, with:
gdb::optional<int> function ()
{
gdb::optional<int> opt;
....
return opt;
we get:
src/gdb/foo.c: In function ‘gdb::optional<int> foo()’:
src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’
return opt;
^
In file included from src/gdb/foo.c:68:0:
src/gdb/common/gdb_optional.h:53:3: note: declared here
optional (const optional &other) = delete;
^
I started by fixing that, and then ran into another missing feature,
also fixed by this patch.
The next feature I'm missing most from gdb::optional<T> compared to
std::optional<T> is construction/move/assignment from a T, instead of
having to default construct an gdb::optional and then use
optional::emplace(....).
For example:
gdb::optional<std::string> function ()
{
gdb::optional<std::string> opt;
std::string str;
...
opt.emplace (std::move (str));
return opt;
vs
gdb::optional<std::string> function ()
{
std::string str;
...
return str;
The copy/move ctor/assign methods weren't initialy implemented because
std::optional supports construction from a type U if U is convertible
to T too, and has rules to decide whether the ctors are
explicit/implicit based on that, and rules for whether the ctor should
be trivial or not, etc., which leads to a much more complicated
implementation.
If we stick to supporting copy/move construction/assignment of/to an
optional<T> from exactly only optional<T> and T, then all that
conversion-related complication disappears, and we still gain
convenience in most use cases.
The patch also makes emplace return a reference to the constructor
object, per C++17 std::optional, and adds a reset method, againt
because std::optional has one and it's trivial to support it. These
two changes are a requirement of the gdb::optional unit testing patch
that will follow.
gdb/ChangeLog:
2017-04-18 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h: Include common/traits.h.
(in_place_t): New type.
(in_place): New constexpr variable.
(optional::optional): Remove member initialization of
m_instantiated.
(optional::optional(in_place_t...)): New constructor.
(optional::~optional): Use reset.
(optional::optional(const optional&)): New.
(optional::optional(const optional&&)): New.
(optional::optional(T &)): New.
(optional::optional(T &&)): New.
(operator::operator=(const optional &)): New.
(operator::operator=(optional &&)): New.
(operator::operator= (const T &))
(operator::operator= (T &&))
(operator::emplace (Args &&... args)): Return a T&. Use reset.
(operator::reset): New.
(operator::m_instantiated):: Add in-class initializer.
* common/traits.h: Include <type_traits>.
(struct And): New types.
Diffstat (limited to 'gdb/common/traits.h')
-rw-r--r-- | gdb/common/traits.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/gdb/common/traits.h b/gdb/common/traits.h index 4b7bac3..c8f29cc 100644 --- a/gdb/common/traits.h +++ b/gdb/common/traits.h @@ -18,6 +18,8 @@ #ifndef COMMON_TRAITS_H #define COMMON_TRAITS_H +#include <type_traits> + namespace gdb { /* Pre C++14-safe (CWG 1558) version of C++17's std::void_t. See @@ -29,6 +31,30 @@ struct make_void { typedef void type; }; template<typename... Ts> using void_t = typename make_void<Ts...>::type; +/* A few trait helpers, mainly stolen from libstdc++. Uppercase + because "and" is a keyword. */ + +template<typename...> +struct And; + +template<> +struct And<> : public std::true_type +{}; + +template<typename B1> +struct And<B1> : public B1 +{}; + +template<typename B1, typename B2> +struct And<B1, B2> + : public std::conditional<B1::value, B2, B1>::type +{}; + +template<typename B1, typename B2, typename B3, typename... Bn> +struct And<B1, B2, B3, Bn...> + : public std::conditional<B1::value, And<B2, B3, Bn...>, B1>::type +{}; + } #endif /* COMMON_TRAITS_H */ |