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