The Battle for Wesnoth  1.15.1+dev
const_clone.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <type_traits>
18 
19 namespace utils
20 {
21 /**
22  * Helper struct to clone the constness of one type to another.
23  *
24  * @warning It seems @c *this in a const member function is not a const object,
25  * use @c this, which is a pointer to a const object.
26  *
27  * @tparam D The destination type, it should have no
28  * cv-qualifier and not be a pointer or
29  * reference.
30  * @tparam S The source type, this type may be a pointer
31  * or reference and obviously is allowed to have
32  * a cv-qualifier, although @c volatile has no
33  * effect.
34  */
35 template<typename D, typename S>
37 {
38  static const bool is_source_const =
39  std::is_const<
40  std::remove_pointer_t<
41  std::remove_reference_t<S>
42  >
43  >::value;
44 
45  /** The destination type, possibly const qualified. */
46  using type =
47  std::conditional_t<is_source_const, const D, D>;
48 
49  /** A reference to the destination type, possibly const qualified. */
50  using reference =
51  std::conditional_t<is_source_const, const D&, D&>;
52 
53  /** A pointer to the destination type, possibly const qualified. */
54  using pointer =
55  std::conditional_t<is_source_const, const D*, D*>;
56 };
57 
58 template<typename D, typename S>
60 
61 template<typename D, typename S>
63 
64 template<typename D, typename S>
66 
67 } // namespace utils
Helper struct to clone the constness of one type to another.
Definition: const_clone.hpp:36
std::conditional_t< is_source_const, const T *, T *> pointer
A pointer to the destination type, possibly const qualified.
Definition: const_clone.hpp:55
std::conditional_t< is_source_const, const T &, T &> reference
A reference to the destination type, possibly const qualified.
Definition: const_clone.hpp:51
static const bool is_source_const
Definition: const_clone.hpp:38
typename const_clone< D, S >::reference const_clone_ref
Definition: const_clone.hpp:62
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:65
std::conditional_t< is_source_const, const T, T > type
The destination type, possibly const qualified.
Definition: const_clone.hpp:47
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:59