Skip to content

Commit 19ada47

Browse files
committed
Add some support for C++17-23
Add `CPPStandard.h` for checking C++ features support through feature test macros, replace deprecated `std::result_of` with a custom template.
1 parent 1bb5e9e commit 19ada47

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

src/common/CPPStandard.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
// CPPStandard.h
35+
36+
#ifndef CPPSTANDARD_H
37+
#define CPPSTANDARD_H
38+
39+
#undef CPP_AGGREGATE
40+
#undef CPP_CONCEPTS
41+
#undef CPP_CONSTEVAL
42+
#undef CPP_CONSTEXPR
43+
#undef CPP_CTAD
44+
#undef CPP_DESIGNATED_INIT
45+
#undef CPP_INVOKE_RESULT
46+
#undef CPP_ATOMIC_WAIT_NOTIFY
47+
#undef CPP_SOURCE_LOCATION
48+
#undef CPP_STACKTRACE
49+
50+
#if defined( __cpp_aggregate_bases ) && defined ( __cpp_aggregate_nsdmi ) && defined ( __cpp_aggregate_paren_init )
51+
#define CPP_AGGREGATE
52+
#endif
53+
54+
#if __cpp_concepts >= 201907L
55+
#define CPP_CONCEPTS
56+
#endif
57+
58+
#if __cpp_consteval >= 201811L
59+
#define CPP_CONSTEVAL
60+
#endif
61+
62+
#if __cpp_constexpr >= 202110L && __cpp_if_constexpr >= 201606L
63+
#define CPP_CONSTEXPR
64+
#endif
65+
66+
#if __cpp_deduction_guides >= 201907L
67+
#define CPP_CTAD
68+
#endif
69+
70+
#if __cpp_designated_initializers >= 201707L
71+
#define CPP_DESIGNATED_INIT
72+
#endif
73+
74+
#if __cpp_lib_is_invocable >= 201703L
75+
#define CPP_INVOKE_RESULT
76+
#endif
77+
78+
#if __cpp_lib_atomic_wait >= 201907L
79+
#define CPP_ATOMIC_WAIT_NOTIFY
80+
#endif
81+
82+
#if __cpp_lib_source_location >= 201907L
83+
#define CPP_SOURCE_LOCATION
84+
#endif
85+
86+
#if __cpp_lib_stacktrace >= 202011L
87+
#define CPP_STACKTRACE
88+
#endif
89+
90+
#endif // CPPSTANDARD_H

src/common/math/Vector.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ along with Daemon Source Code. If not, see <http://www.gnu.org/licenses/>.
2626
#define COMMON_VECTOR_H_
2727

2828
#include <ostream>
29+
#include <type_traits>
30+
31+
template<typename Func, typename... Args> using resultof_t = decltype( std::declval<Func>()( std::declval<Args...> ) );
2932

3033
namespace Math {
3134

@@ -87,9 +90,9 @@ namespace Math {
8790
void Store(Type* ptr) const;
8891

8992
// Apply a function on all elements of the vector
90-
template<typename Func> Vector<Dimension, typename std::result_of<Func(Type)>::type> Apply(Func func) const;
91-
template<typename Func, typename Type2> Vector<Dimension, typename std::result_of<Func(Type, Type2)>::type> Apply2(Func func, Vector<Dimension, Type2> other) const;
92-
template<typename Func, typename Type2, typename Type3> Vector<Dimension, typename std::result_of<Func(Type, Type2, Type3)>::type> Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const;
93+
template<typename Func> Vector<Dimension, resultof_t<Func, Type>> Apply(Func func) const;
94+
template<typename Func, typename Type2> Vector<Dimension, resultof_t<Func, Type, Type2>> Apply2(Func func, Vector<Dimension, Type2> other) const;
95+
template<typename Func, typename Type2, typename Type3> Vector<Dimension, resultof_t<Func, Type, Type2, Type3>> Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const;
9396

9497
// Reduce the elements of the vector to a single value
9598
template<typename Func> Type Reduce(Type init, Func func);
@@ -843,25 +846,25 @@ namespace Math {
843846

844847
// VectorBase::Apply/Apply2/Apply3
845848
template<size_t Dimension, typename Type> template<typename Func>
846-
Vector<Dimension, typename std::result_of<Func(Type)>::type> VectorBase<Dimension, Type>::Apply(Func func) const
849+
Vector<Dimension, resultof_t<Func, Type>> VectorBase<Dimension, Type>::Apply(Func func) const
847850
{
848-
Vector<Dimension, typename std::result_of<Func(Type)>::type> out;
851+
Vector<Dimension, resultof_t<Func, Type>> out;
849852
for (size_t i = 0; i < Dimension; i++)
850853
out.data[i] = func(data[i]);
851854
return out;
852855
}
853856
template<size_t Dimension, typename Type> template<typename Func, typename Type2>
854-
Vector<Dimension, typename std::result_of<Func(Type, Type2)>::type> VectorBase<Dimension, Type>::Apply2(Func func, Vector<Dimension, Type2> other) const
857+
Vector<Dimension, resultof_t<Func, Type, Type2>> VectorBase<Dimension, Type>::Apply2(Func func, Vector<Dimension, Type2> other) const
855858
{
856-
Vector<Dimension, typename std::result_of<Func(Type, Type2)>::type> out;
859+
Vector<Dimension, resultof_t<Func(Type, Type2)>> out;
857860
for (size_t i = 0; i < Dimension; i++)
858861
out.data[i] = func(data[i], other.data[i]);
859862
return out;
860863
}
861864
template<size_t Dimension, typename Type> template<typename Func, typename Type2, typename Type3>
862-
Vector<Dimension, typename std::result_of<Func(Type, Type2, Type3)>::type> VectorBase<Dimension, Type>::Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const
865+
Vector<Dimension, resultof_t<Func, Type, Type2, Type3>> VectorBase<Dimension, Type>::Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const
863866
{
864-
Vector<Dimension, typename std::result_of<Func(Type, Type2, Type3)>::type> out;
867+
Vector<Dimension, resultof_t<Func, Type, Type2, Type3>> out;
865868
for (size_t i = 0; i < Dimension; i++)
866869
out.data[i] = func(data[i], other1.data[i], other2.data[i]);
867870
return out;

0 commit comments

Comments
 (0)