diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ef2faad79b387f258420a1c35d8b216a18e967..0eedf74635917ed06aeda3105ccc5e5475a7641c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,9 @@ In order to build the DUNE core modules you need at least the following software - Added the methods `checkThrow`,`requireThrow` and the corresponding `checkNoThrow`, `requireNoThrow` to the `Dune::TestSuite` to test for throwing and no throwing of exceptions. +- Add the utility `IsCompileTimeConstant` to check for integral constants and anything with + the same interface. + ## Build System - Remove deprecated `add_directory_test_target` function. diff --git a/dune/common/hybridutilities.hh b/dune/common/hybridutilities.hh index 25c17ccfac7943e5f25e7fd3ba5f7dccf4f78063..2c120286fae186140ea71b89f128911ad294d21d 100644 --- a/dune/common/hybridutilities.hh +++ b/dune/common/hybridutilities.hh @@ -448,7 +448,7 @@ public: template<class... Args> constexpr decltype(auto) operator()(const Args&... args) const { - if constexpr (std::conjunction_v<IsIntegralConstant<Args>...>) + if constexpr (std::conjunction_v<IsCompileTimeConstant<Args>...>) { constexpr auto result = _functor(Args::value...); // apply functor on integral constant arguments and return an integral constant of the result diff --git a/dune/common/test/CMakeLists.txt b/dune/common/test/CMakeLists.txt index c195d83e7e03e0ca9678c27eeaee82f3d2e3d93e..72208ede42efa7142e468ceeda834237d063f709 100644 --- a/dune/common/test/CMakeLists.txt +++ b/dune/common/test/CMakeLists.txt @@ -390,6 +390,9 @@ dune_add_test(SOURCES transposetest.cc dune_add_test(SOURCES tupleutilitytest.cc LABELS quick) +dune_add_test(SOURCES typetraitstest.cc + LABELS quick) + dune_add_test(SOURCES typeutilitytest.cc LABELS quick) diff --git a/dune/common/test/typetraitstest.cc b/dune/common/test/typetraitstest.cc new file mode 100644 index 0000000000000000000000000000000000000000..bdc628e6ba9df991e9d56eac327ed54e9696cf08 --- /dev/null +++ b/dune/common/test/typetraitstest.cc @@ -0,0 +1,41 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root +// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <type_traits> +#include <dune/common/indices.hh> +#include <dune/common/typetraits.hh> + +// no types and additional constexpr functions are defined +struct A { + static constexpr int value = 42; +}; + +// non constexpr implementation +struct B { + using type = B; + using value_type = int; + static int value; + operator int () const { return 42; } + int operator()() const { return 42; } +}; +int B::value = 42; + +int main() +{ + using namespace Dune; + + static_assert(IsCompileTimeConstant<std::integral_constant<int,5>>::value); + static_assert(IsCompileTimeConstant<index_constant<5>>::value); + static_assert(IsCompileTimeConstant<std::bool_constant<true>>::value); + static_assert(IsCompileTimeConstant<std::is_same<int,double>>::value); + + static_assert(not IsCompileTimeConstant<int>::value); + static_assert(not IsCompileTimeConstant<A>::value); + static_assert(not IsCompileTimeConstant<B>::value); +} \ No newline at end of file diff --git a/dune/common/typetraits.hh b/dune/common/typetraits.hh index 66496619aee379ce58976a14e45fd16bd192e61c..542b97d8e22857231653cb3103d4a71a1c59e620 100644 --- a/dune/common/typetraits.hh +++ b/dune/common/typetraits.hh @@ -384,6 +384,33 @@ namespace Dune {}; +#ifndef DOXYGEN + + namespace Impl { + + struct IsCompileTimeConstant + { + template <class T, T value> + static std::true_type check(std::integral_constant<T,value>); + static std::false_type check(...); + }; + + } // namespace Impl + +#endif // DOXYGEN + + /** + * \brief Check if T is an integral constant or any type + * derived from `std::integral_constant`. + * + * The result is exported by deriving from std::true_type or std::false_type. + */ + template<class T> + struct IsCompileTimeConstant + : public decltype(Impl::IsCompileTimeConstant::check(std::declval<T>())) + {}; + + /** * \brief Compute size of variadic type list