dune-functions 2.9.0
type_traits.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_COMMON_TYPE_TRAITS_HH
4#define DUNE_FUNCTIONS_COMMON_TYPE_TRAITS_HH
5
6#include <type_traits>
7
8#include <dune/common/typeutilities.hh>
9
10namespace Dune {
11namespace Functions {
12
13
24template<class T, class... Args>
25using enableIfConstructible = typename std::enable_if<
26 std::is_constructible<T, Args...>::value, int>::type;
27
28
29
30namespace Imp {
31
32 // As a last resort try if there's a static constexpr size()
33 template<class T>
34 constexpr auto staticSize(const T*, const PriorityTag<0>&)
35 -> decltype(std::integral_constant<std::size_t,T::size()>())
36 {
37 return {};
38 }
39
40 // Try if class has constexpr default constructor and size method
41 template<class T>
42 constexpr auto staticSize(const T*, const PriorityTag<1>&)
43 -> decltype(std::integral_constant<std::size_t,T().size()>())
44 {
45 return {};
46 }
47
48 // Try if tuple_size is implemented for class
49 template<class T>
50 constexpr auto staticSize(const T*, const PriorityTag<2>&)
51 -> decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>())
52 {
53 return {};
54 }
55
56 template<class T>
57 constexpr std::false_type hasStaticSize(const T* t, const PriorityTag<0>& p)
58 {
59 return {};
60 }
61
62 template<class T>
63 constexpr auto hasStaticSize(const T* t, const PriorityTag<1>& p)
64 -> decltype(staticSize(t ,PriorityTag<42>()), std::true_type())
65 {
66 return {};
67 }
68
69}
70
71
72
80template<class T>
82 public decltype(Imp::hasStaticSize((typename std::decay<T>::type*)(nullptr), PriorityTag<42>()))
83{};
84
85
86
94template<class T>
95struct StaticSize :
96 public decltype(Imp::staticSize((typename std::decay<T>::type*)(nullptr), PriorityTag<42>()))
97{};
98
99
100
101}} // namespace Dune::Functions
102
103#endif // DUNE_FUNCTIONS_COMMON_TYPE_TRAITS_HH
typename std::enable_if< std::is_constructible< T, Args... >::value, int >::type enableIfConstructible
Helper to constrain forwarding constructors.
Definition: type_traits.hh:26
Definition: polynomial.hh:10
Check if type is a statically sized container.
Definition: type_traits.hh:83
Obtain size of statically sized container.
Definition: type_traits.hh:97