mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
typeTraits.hpp
Go to the documentation of this file.
1/** \file typeTraits.hpp
2 * \brief A collection of type trait evaluations.
3 * \ingroup utils_files
4 * \author Jared R. Males (jaredmales@gmail.com)
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2015, 2016, 2017, 2018 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef typeTraits_hpp
28#define typeTraits_hpp
29
30#include <type_traits>
31#include <vector>
32
33#include "trueFalseT.hpp"
34
35namespace mx
36{
37
38namespace meta
39{
40
41template <typename... Ts>
42using void_t = void;
43
44template <typename T, typename = void>
45struct has_value_type : trueFalseT<false>
46{
47};
48
49/// Test whether a type has a typedef of "value_type"
50/** Used for compile-time determination of type.
51 *
52 * Example usage:
53 * \code
54 * bool has_vt = has_value_type<std::vector<float> >; //Evaluates to true
55 * bool has_not_vt = has_value_type<float>; //Evaluates to false
56 * \endcode
57 *
58 * This was taken directly from the example at http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
59 *
60 * \ingroup meta
61 */
62template <typename T>
63struct has_value_type<T, void_t<typename T::value_type>> : trueFalseT<true>
64{
65};
66
67/// Check whether a type is std::vector or not.
68/** First resolves whether the type as a typedef value_type. If not, then member value will be false.
69 * If it does have a member type value_type, then it uses std::is_same to compare the type to std::vector.
70 * Then value is equal to std::is_same<T, std::vector<typenameT::value_type>>::value.
71 *
72 * \tparam T is the type to evaulate for vector-ness.
73 * \tparam has_value_type is a boolean type based on has_value_type SFINAE test.
74 *
75 * \ingroup meta
76 */
77template <typename T, bool if_value_type = has_value_type<T>::value>
79
80/// Partial specialization for the case with value_type member type in T, invoking std::is_same.
81/** \ingroup meta
82 */
83template <typename T>
84struct is_std_vector<T, true>
85{
86 static const bool value = std::is_same<T, std::vector<typename T::value_type>>::value;
87};
88
89/// Partial specialization for the case with no value_type member type in T.
90/** \ingroup meta
91 */
92template <typename T>
93struct is_std_vector<T, false>
94{
95 static const bool value = false;
96};
97
98} // namespace meta
99} // namespace mx
100
101#endif // typeTraits_hpp
The mxlib c++ namespace.
Definition mxlib.hpp:37
Check whether a type is std::vector or not.
Template declaration of a trueFalseT type.
Declares and defines a true-false virtual type used for boolean template overrides.