deal.II version GIT relicensing-7075-gaa980f2bfe 2026-11-16 20:40:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
template_constraints.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2003 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_template_constraints_h
14#define dealii_template_constraints_h
15
16
17#include <deal.II/base/config.h>
18
22
24
25#include <complex>
26#include <type_traits>
27#include <utility>
28
29
31
32// Detection idiom adapted from Version 2 of the C++ Extensions for Library
33// Fundamentals, ISO/IEC TS 19568:2017
34namespace internal
35{
41 namespace SupportsOperation
42 {
43 template <class...>
44 using void_t = void;
45
52 template <class Default,
53 class AlwaysVoid,
54 template <class...> class Op,
55 class... Args>
56 struct detector
57 {
58 using value_t = std::false_type;
59 using type = Default;
60 };
61
74 template <class Default, template <class...> class Op, class... Args>
75 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
76 {
77 using value_t = std::true_type;
78 using type = Op<Args...>;
79 };
80
81
87 {};
88
93 struct nonesuch : private nonesuch_base
94 {
95 ~nonesuch() = delete;
96 nonesuch(const nonesuch &) = delete;
97 void
98 operator=(const nonesuch &) = delete;
99 };
100
101 template <class Default, template <class...> class Op, class... Args>
102 using detected_or = detector<Default, void, Op, Args...>;
103
104 template <template <class...> class Op, class... Args>
105 using is_detected = typename detected_or<nonesuch, Op, Args...>::value_t;
106
107 template <template <class...> class Op, class... Args>
108 using detected_t = typename detected_or<nonesuch, Op, Args...>::type;
109
110 template <class Default, template <class...> class Op, class... Args>
111 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
112
113 template <class Expected, template <class...> class Op, class... Args>
114 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
115
116 template <class To, template <class...> class Op, class... Args>
118 std::is_convertible<detected_t<Op, Args...>, To>;
119 } // namespace SupportsOperation
120
121
156 template <template <class...> class Op, class... Args>
157 constexpr bool is_supported_operation =
159} // namespace internal
160
161
162
163namespace internal
164{
165 namespace TemplateConstraints
166 {
175 template <bool... Values>
176 struct all_true
177 {
178 static constexpr bool value = (Values && ...);
179 };
180
181
186 template <bool... Values>
187 struct any_true
188 {
189 static constexpr bool value = (Values || ...);
190 };
191 } // namespace TemplateConstraints
192} // namespace internal
193
200template <class Base, class... Derived>
202{
204 std::is_base_of_v<Base, Derived>...>::value;
205};
206
207
208
215template <typename Type, class... Types>
217{
219 std::is_same_v<Type, Types>...>::value;
220};
221
222
223
230template <typename Type, class... Types>
232{
234 std::is_same_v<Type, Types>...>::value;
235};
236
237
238
239/*
240 * A generalization of `std::enable_if` that only works if
241 * <i>all</i> of the given boolean template parameters are
242 * true. See [here](https://en.cppreference.com/w/cpp/types/enable_if)
243 * for what `std::enable_if` does.
244 *
245 * @note
246 * In contrast to `std::enable_if`, this template has no additional
247 * template type (which for `std::enable_if` is defaulted to `void`).
248 * As a consequence, this structure cannot be used for anything other
249 * than enabling or disabling a template declaration; in particular
250 * it cannot be used to set the return type of a function as one might
251 * do with something like
252 * @code
253 * template <typename T>
254 * typename std::enable_if<std::is_floating_point_v<T>, T>::type
255 * abs (const T t);
256 * @endcode
257 * which declares a function template `abs()` that can only be
258 * instantiated if `T` is a floating point type; this function then
259 * returns an object of type `T` as indicated by the last argument to
260 * `std::enable_if`. The reason `enable_if_all` does not allow providing
261 * this additional type is that variadic templates (here, the list of
262 * `bool` arguments) must be the last template argument.
263 */
264template <bool... Values>
266 : std::enable_if<internal::TemplateConstraints::all_true<Values...>::value>
267{};
268
269
270
271/*
272 * A generalization of `std::enable_if_t` that only works if
273 * <i>all</i> of the given boolean template parameters are
274 * true. See [here](https://en.cppreference.com/w/cpp/types/enable_if)
275 * for what `std::enable_if_t` does.
276 *
277 * @note
278 * In contrast to `std::enable_if_t`, this template has no additional
279 * template type (which for `std::enable_if` is defaulted to `void`).
280 * As a consequence, this structure cannot be used for anything other
281 * than enabling or disabling a template declaration; in particular
282 * it cannot be used to set the return type of a function as one might
283 * do with something like
284 * @code
285 * template <typename T>
286 * std::enable_if_t<std::is_floating_point_v<T>, T>
287 * abs (const T t);
288 * @endcode
289 * which declares a function template `abs()` that can only be
290 * instantiated if `T` is a floating point type; this function then
291 * returns an object of type `T` as indicated by the last argument to
292 * `std::enable_if`. The reason `enable_if_all` does not allow providing
293 * this additional type is that variadic templates (here, the list of
294 * `bool` arguments) must be the last template argument.
295 */
296template <bool... Values>
297using enable_if_all_t = typename enable_if_all<Values...>::type;
298
299
305template <typename T>
307 decltype(std::begin(std::declval<T>()), std::end(std::declval<T>()));
308
309template <typename T>
310constexpr bool has_begin_and_end =
311 internal::is_supported_operation<begin_and_end_t, T>;
312
313
319template <typename ArgType, typename ValueType>
321{
322 ValueType value;
323 ValueType
324 operator()(const ArgType &)
325 {
326 return value;
327 }
328};
329
330
331
349{
354 template <typename T>
355 static bool
356 equal(const T *p1, const T *p2)
357 {
358 return (p1 == p2);
359 }
360
361
368 template <typename T, typename U>
369 static bool
370 equal(const T *, const U *)
371 {
372 return false;
373 }
374};
375
376
377
378namespace internal
379{
390 template <typename T, typename U>
392 {
393 using type = decltype(std::declval<T>() * std::declval<U>());
394 };
395
396} // namespace internal
397
398
399
446template <typename T, typename U>
448{
449 using type =
450 typename internal::ProductTypeImpl<std::decay_t<T>, std::decay_t<U>>::type;
451};
452
453namespace internal
454{
455 // Annoyingly, there is no std::complex<T>::operator*(U) for scalars U
456 // other than T (not even in C++11, or C++14). We provide our own overloads
457 // in base/complex_overloads.h, but in order for them to work, we have to
458 // manually specify all products we want to allow:
459
460 template <typename T>
461 struct ProductTypeImpl<std::complex<T>, std::complex<T>>
462 {
463 using type = std::complex<T>;
464 };
465
466 template <typename T, typename U>
467 struct ProductTypeImpl<std::complex<T>, std::complex<U>>
468 {
469 using type = std::complex<typename ProductType<T, U>::type>;
470 };
471
472 template <typename U>
473 struct ProductTypeImpl<double, std::complex<U>>
474 {
475 using type = std::complex<typename ProductType<double, U>::type>;
476 };
477
478 template <typename T>
479 struct ProductTypeImpl<std::complex<T>, double>
480 {
481 using type = std::complex<typename ProductType<T, double>::type>;
482 };
483
484 template <typename U>
485 struct ProductTypeImpl<float, std::complex<U>>
486 {
487 using type = std::complex<typename ProductType<float, U>::type>;
488 };
489
490 template <typename T>
491 struct ProductTypeImpl<std::complex<T>, float>
492 {
493 using type = std::complex<typename ProductType<T, float>::type>;
494 };
495
496} // namespace internal
497
498
499
555template <typename T>
557
558
559template <>
560struct EnableIfScalar<double>
561{
562 using type = double;
563};
564
565template <>
566struct EnableIfScalar<float>
567{
568 using type = float;
569};
570
571template <>
572struct EnableIfScalar<long double>
573{
574 using type = long double;
575};
576
577template <>
579{
580 using type = int;
581};
582
583template <>
584struct EnableIfScalar<unsigned int>
585{
586 using type = unsigned int;
587};
588
589template <typename T>
590struct EnableIfScalar<std::complex<T>>
591{
592 using type = std::complex<T>;
593};
594
595
596// Forward declarations of vector types
597template <typename Number>
598class Vector;
599
600template <typename Number>
601class BlockVector;
602
604{
605 template <typename Number>
606 class ReadWriteVector;
607
608 namespace distributed
609 {
610 template <typename Number, typename MemorySpace>
611 class Vector;
612
613 template <typename Number, typename MemorySpace>
614 class BlockVector;
615 } // namespace distributed
616} // namespace LinearAlgebra
617
618#ifdef DEAL_II_WITH_PETSC
620{
621 class VectorBase;
622 class Vector;
623 class BlockVector;
624
625 namespace MPI
626 {
627 class Vector;
628 class BlockVector;
629
630 class SparseMatrix;
631 class BlockSparseMatrix;
632 } // namespace MPI
633} // namespace PETScWrappers
634#endif
635
636#ifdef DEAL_II_WITH_TRILINOS
637# if defined(DEAL_II_TRILINOS_WITH_EPETRA)
639{
640 namespace MPI
641 {
642 class Vector;
643 class BlockVector;
644 } // namespace MPI
645} // namespace TrilinosWrappers
646
647namespace LinearAlgebra
648{
649 namespace EpetraWrappers
650 {
651 class Vector;
652 }
653} // namespace LinearAlgebra
654# endif
655
656# ifdef DEAL_II_TRILINOS_WITH_TPETRA
657namespace LinearAlgebra
658{
659 namespace TpetraWrappers
660 {
661 template <typename Number, typename MemorySpace>
662 class Vector;
663
664 template <typename Number, typename MemorySpace>
665 class BlockVector;
666 } // namespace TpetraWrappers
667} // namespace LinearAlgebra
668# endif
669#endif
670
671
672
677namespace concepts
678{
679#if defined(DEAL_II_HAVE_CXX20) || defined(DOXYGEN)
690 template <typename C>
691 concept is_contiguous_container = requires(C &c) {
692 { std::data(c) };
693 { std::size(c) };
694 };
695
696
706 template <int dim, int spacedim>
708 (dim >= 1 && spacedim <= 3 && dim <= spacedim);
709
710 namespace internal
711 {
718 template <typename T>
719 inline constexpr bool is_dealii_vector_type = false;
720
721 template <typename Number>
722 inline constexpr bool is_dealii_vector_type<::Vector<Number>> = true;
723
724 template <typename Number>
725 inline constexpr bool is_dealii_vector_type<::BlockVector<Number>> =
726 true;
727
728 template <typename Number>
729 inline constexpr bool
731 true;
732
733 template <typename Number, typename MemorySpace>
734 inline constexpr bool is_dealii_vector_type<
736
737 template <typename Number, typename MemorySpace>
738 inline constexpr bool is_dealii_vector_type<
740 true;
741
742# ifdef DEAL_II_WITH_PETSC
743 template <>
745 true;
746
747 template <>
748 inline constexpr bool
750
751 template <>
752 inline constexpr bool
754
755 template <>
756 inline constexpr bool
758# endif
759
760# ifdef DEAL_II_WITH_TRILINOS
761# if defined(DEAL_II_TRILINOS_WITH_EPETRA)
762 template <>
763 inline constexpr bool
765
766 template <>
767 inline constexpr bool
769
770 template <>
771 inline constexpr bool
773 true;
774# endif
775
776# ifdef DEAL_II_TRILINOS_WITH_TPETRA
777 template <typename Number, typename MemorySpace>
778 inline constexpr bool is_dealii_vector_type<
780 true;
781
782 template <typename Number, typename MemorySpace>
783 inline constexpr bool is_dealii_vector_type<
785 true;
786# endif
787# endif
788
789
797 template <typename T>
798 inline constexpr bool is_dealii_petsc_vector_type = false;
799
800# ifdef DEAL_II_WITH_PETSC
801 template <>
802 inline constexpr bool
804
805 template <>
806 inline constexpr bool
808
809 template <>
810 inline constexpr bool
812
813 template <>
814 inline constexpr bool
816
817 template <>
818 inline constexpr bool
820 true;
821# endif
822
823
831 template <typename T>
832 inline constexpr bool is_dealii_petsc_matrix_type = false;
833
834# ifdef DEAL_II_WITH_PETSC
835 template <>
836 inline constexpr bool
838 true;
839
840 template <>
841 inline constexpr bool is_dealii_petsc_matrix_type<
843# endif
844 } // namespace internal
845
846
858 template <typename VectorType>
860 internal::is_dealii_vector_type<std::remove_cv_t<VectorType>>;
861
871 template <typename VectorType>
873 is_dealii_vector_type<VectorType> && (std::is_const_v<VectorType> == false);
874
883 template <typename VectorType>
885 internal::is_dealii_petsc_vector_type<VectorType>;
886
895 template <typename VectorType>
897 internal::is_dealii_petsc_matrix_type<VectorType>;
898#endif
899} // namespace concepts
900
901
902template <int dim, int spacedim>
904class Triangulation;
905
906template <int dim, int spacedim>
908class DoFHandler;
909
910namespace parallel
911{
912 namespace distributed
913 {
914 template <int dim, int spacedim>
916 class Triangulation;
917 }
918 namespace shared
919 {
920 template <int dim, int spacedim>
922 class Triangulation;
923 }
924 namespace fullydistributed
925 {
926 template <int dim, int spacedim>
928 class Triangulation;
929 }
930} // namespace parallel
931
932namespace concepts
933{
934 namespace internal
935 {
943 template <class T>
944 inline constexpr bool is_distributed_vector_type = false;
945
946 // ---- deal.II LA distributed vectors ----
947 template <typename Number, typename MemorySpace>
948 inline constexpr bool is_distributed_vector_type<
950
951 template <typename Number, typename MemorySpace>
952 inline constexpr bool is_distributed_vector_type<
954 true;
955
956#ifdef DEAL_II_TRILINOS_WITH_EPETRA
957 template <>
958 inline constexpr bool
959 is_distributed_vector_type<::TrilinosWrappers::MPI::Vector> = true;
960
961 template <>
962 inline constexpr bool
963 is_distributed_vector_type<::TrilinosWrappers::MPI::BlockVector> =
964 true;
965#endif
966
967#ifdef DEAL_II_TRILINOS_WITH_TPETRA
968 template <typename Number, typename MemorySpace>
969 inline constexpr bool is_distributed_vector_type<
971 true;
972
973 template <typename Number, typename MemorySpace>
974 inline constexpr bool is_distributed_vector_type<
976 true;
977#endif
978
979#ifdef DEAL_II_WITH_PETSC
980 template <>
981 inline constexpr bool
982 is_distributed_vector_type<::PETScWrappers::MPI::Vector> = true;
983
984 template <>
985 inline constexpr bool
986 is_distributed_vector_type<::PETScWrappers::MPI::BlockVector> =
987 true;
988#endif
989 } // namespace internal
990
991#if defined(DEAL_II_HAVE_CXX20) || defined(DOXYGEN)
992 namespace internal
993 {
994 template <typename T>
995 inline constexpr bool is_triangulation_or_dof_handler = false;
996
997 template <int dim, int spacedim>
998 inline constexpr bool
1000
1001 template <int dim, int spacedim>
1002 inline constexpr bool is_triangulation_or_dof_handler<
1004
1005 template <int dim, int spacedim>
1006 inline constexpr bool is_triangulation_or_dof_handler<
1008
1009 template <int dim, int spacedim>
1010 inline constexpr bool is_triangulation_or_dof_handler<
1012
1013 template <int dim, int spacedim>
1014 inline constexpr bool
1016 } // namespace internal
1017
1018
1026 template <typename MeshType>
1028 internal::is_triangulation_or_dof_handler<MeshType>;
1029
1036 template <typename VectorType>
1037 concept is_vector_space_vector = requires(VectorType U,
1038 VectorType V,
1039 VectorType W,
1040 typename VectorType::value_type a,
1041 typename VectorType::value_type b,
1042 typename VectorType::value_type s,
1043 VectorOperation::values operation) {
1044 // Check local type requirements:
1045 typename VectorType::value_type;
1046 typename VectorType::size_type;
1047 typename VectorType::real_type;
1048
1049 // Check some assignment and reinit operations;
1050 U.reinit(V);
1051 U.reinit(V, /* omit_zeroing_entries= */ true);
1052
1053 U = V;
1054 U = a; // assignment of scalar
1055
1056 U.equ(a, V);
1057
1058 // Check scaling operations
1059 U *= a;
1060 U /= a;
1061
1062 U.scale(V);
1063
1064 // Vector additions:
1065 U += V;
1066 U -= V;
1067
1068 U.add(a);
1069 U.add(a, V);
1070 U.add(a, V, b, W);
1071
1072 U.sadd(s, a, V);
1073
1074 // Norms and similar stuff:
1075 { U.mean_value() } -> std::convertible_to<typename VectorType::value_type>;
1076
1077 { U.l1_norm() } -> std::convertible_to<typename VectorType::real_type>;
1078
1079 { U.l2_norm() } -> std::convertible_to<typename VectorType::real_type>;
1080
1081 { U.linfty_norm() } -> std::convertible_to<typename VectorType::real_type>;
1082
1083 // Dot products:
1084 { U * V } -> std::convertible_to<typename VectorType::value_type>;
1085
1086 {
1087 U.add_and_dot(a, V, W)
1088 } -> std::convertible_to<typename VectorType::value_type>;
1089
1090 // Some queries:
1091 { U.size() } -> std::convertible_to<typename VectorType::size_type>;
1092
1093 { U.all_zero() } -> std::same_as<bool>;
1094
1095 { U.get_mpi_communicator() } -> std::same_as<MPI_Comm>;
1096
1097 // Synchronization:
1098 { U.compress(operation) } -> std::same_as<void>;
1099 };
1100
1101
1110 template <typename MatrixType, typename VectorType>
1112 requires(const MatrixType &A, VectorType &dst, const VectorType &src) {
1113 A.vmult(dst, src);
1114 };
1115
1116
1125 template <typename MatrixType, typename VectorType>
1127 requires(const MatrixType &A, VectorType &dst, const VectorType &src) {
1128 A.Tvmult(dst, src);
1129 };
1130
1131#endif
1132
1133} // namespace concepts
1134
1135
1136
1138
1139#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
constexpr bool is_distributed_vector_type
typename detected_or< Default, Op, Args... >::type detected_or_t
std::is_convertible< detected_t< Op, Args... >, To > is_detected_convertible
typename detected_or< nonesuch, Op, Args... >::value_t is_detected
typename detected_or< nonesuch, Op, Args... >::type detected_t
std::is_same< Expected, detected_t< Op, Args... > > is_detected_exact
constexpr bool is_supported_operation
STL namespace.
static bool equal(const T *p1, const T *p2)
static bool equal(const T *, const U *)
typename internal::ProductTypeImpl< std::decay_t< T >, std::decay_t< U > >::type type
static constexpr bool value
ValueType operator()(const ArgType &)
std::complex< typename ProductType< double, U >::type > type
std::complex< typename ProductType< float, U >::type > type
std::complex< typename ProductType< T, double >::type > type
std::complex< typename ProductType< T, float >::type > type
std::complex< typename ProductType< T, U >::type > type
decltype(std::declval< T >() *std::declval< U >()) type
void operator=(const nonesuch &)=delete
nonesuch(const nonesuch &)=delete
static constexpr bool value
static constexpr bool value
typename enable_if_all< Values... >::type enable_if_all_t
decltype(std::begin(std::declval< T >()), std::end(std::declval< T >())) begin_and_end_t
constexpr bool has_begin_and_end