version 3.10-dev
isvalid.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
14#ifndef DUMUX_TYPETRAITS_ISVALID_HH
15#define DUMUX_TYPETRAITS_ISVALID_HH
16
17#include <type_traits>
18
19namespace Dumux {
20
21namespace Detail {
22
23// the functor testing an expression for validity
24// Expression: can be for example a lambda expression
25template <typename Expression>
27{
28private:
29 // std::declval creates an object of expression
30 // the expression, i.e. a lambda expression gets an object as a parameter and does checks on it.
31 // so we create also an object of the parameter usiung std::declval
32 // if decltype can evaluate the type, i.e. the object parameter is a valid argument for the expression
33 // we return std::true_type
34 // note: the int is used to give the first overload always precedence
35 // note: the last argument in decltype determines the deduced type but all types need to be valid
36 template <typename Argument>
37 constexpr auto testArgument_(int /* unused int to make this overload the priority choice */) const
38 -> decltype(std::declval<Expression>()(std::declval<Argument>()), std::true_type())
39 { return std::true_type(); }
40
41 // otherwise we return std::false_type, i.e. this is the fallback
42 template <typename Argument>
43 constexpr std::false_type testArgument_(...) const
44 { return std::false_type(); }
45
46public:
47 // the operator () takes the argument we want to use as argument to the test expression
48 // note we use the int to prefer the "valid"-result overload of test_ if possible
49 template <typename Argument>
50 constexpr auto operator() (const Argument& arg) const
51 { return testArgument_<Argument>(int()); }
52
53 // check function takes the template argument explicitly
54 template <typename Argument>
55 constexpr auto check () const
56 { return testArgument_<Argument>(int()); }
57};
58
59} // end namespace Detail
60
61
80template <typename Expression>
81constexpr auto isValid(const Expression& t)
83
84} // end namespace Dumux
85
86#endif
constexpr auto isValid(const Expression &t)
A function that creates a test functor to do class member introspection at compile time.
Definition: isvalid.hh:81
Definition: adapt.hh:17
Definition: isvalid.hh:27
constexpr auto operator()(const Argument &arg) const
Definition: isvalid.hh:50
constexpr auto check() const
Definition: isvalid.hh:55