Skip to content

Commit 21ec2d1

Browse files
committed
Add some support for C++17-23
Add `CPPStandard.h` for checking C++ features support by version, replace deprecated `std::result_of` on >= C++17. Use the `/Zc:__cplusplus` for MSVC CI so it will get the correct version in `CPPStandard`.
1 parent 6ec7fb3 commit 21ec2d1

2 files changed

Lines changed: 109 additions & 9 deletions

File tree

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: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ 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+
#include "common/CPPStandard.h"
32+
33+
// HACK: checking for CPP_INVOKE_RESULT doesn't work on MSVC
34+
#if __cplusplus >= 201703L
35+
#define invoke_result std::invoke_result
36+
#else
37+
#define invoke_result std::result_of
38+
#endif
2939

3040
namespace Math {
3141

@@ -87,9 +97,9 @@ namespace Math {
8797
void Store(Type* ptr) const;
8898

8999
// 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;
100+
template<typename Func> Vector<Dimension, typename invoke_result<Func(Type)>::type> Apply(Func func) const;
101+
template<typename Func, typename Type2> Vector<Dimension, typename invoke_result<Func(Type, Type2)>::type> Apply2(Func func, Vector<Dimension, Type2> other) const;
102+
template<typename Func, typename Type2, typename Type3> Vector<Dimension, typename invoke_result<Func(Type, Type2, Type3)>::type> Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const;
93103

94104
// Reduce the elements of the vector to a single value
95105
template<typename Func> Type Reduce(Type init, Func func);
@@ -843,25 +853,25 @@ namespace Math {
843853

844854
// VectorBase::Apply/Apply2/Apply3
845855
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
856+
Vector<Dimension, typename invoke_result<Func(Type)>::type> VectorBase<Dimension, Type>::Apply(Func func) const
847857
{
848-
Vector<Dimension, typename std::result_of<Func(Type)>::type> out;
858+
Vector<Dimension, typename invoke_result<Func(Type)>::type> out;
849859
for (size_t i = 0; i < Dimension; i++)
850860
out.data[i] = func(data[i]);
851861
return out;
852862
}
853863
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
864+
Vector<Dimension, typename invoke_result<Func(Type, Type2)>::type> VectorBase<Dimension, Type>::Apply2(Func func, Vector<Dimension, Type2> other) const
855865
{
856-
Vector<Dimension, typename std::result_of<Func(Type, Type2)>::type> out;
866+
Vector<Dimension, typename invoke_result<Func(Type, Type2)>::type> out;
857867
for (size_t i = 0; i < Dimension; i++)
858868
out.data[i] = func(data[i], other.data[i]);
859869
return out;
860870
}
861871
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
872+
Vector<Dimension, typename invoke_result<Func(Type, Type2, Type3)>::type> VectorBase<Dimension, Type>::Apply3(Func func, Vector<Dimension, Type2> other1, Vector<Dimension, Type3> other2) const
863873
{
864-
Vector<Dimension, typename std::result_of<Func(Type, Type2, Type3)>::type> out;
874+
Vector<Dimension, typename invoke_result<Func(Type, Type2, Type3)>::type> out;
865875
for (size_t i = 0; i < Dimension; i++)
866876
out.data[i] = func(data[i], other1.data[i], other2.data[i]);
867877
return out;

0 commit comments

Comments
 (0)