Skip to content

Commit afc0a29

Browse files
committed
Polyfills: add decay
1 parent a256ec7 commit afc0a29

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

extras/tests/Misc/TypeTraits.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ TEST_CASE("Polyfills/type_traits") {
211211
CHECK(is_enum<bool>::value == false);
212212
CHECK(is_enum<double>::value == false);
213213
}
214+
215+
SECTION("decay") {
216+
CHECK(is_same<decay_t<int>, int>::value);
217+
CHECK(is_same<decay_t<int&>, int>::value);
218+
CHECK(is_same<decay_t<int&&>, int>::value);
219+
CHECK(is_same<decay_t<int[]>, int*>::value);
220+
CHECK(is_same<decay_t<int[10]>, int*>::value);
221+
CHECK(is_same<decay_t<decltype("toto")>, const char*>::value);
222+
}
214223
}
215224

216225
TEST_CASE("is_std_string") {

src/ArduinoJson/Polyfills/type_traits.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "type_traits/conditional.hpp"
8+
#include "type_traits/decay.hpp"
89
#include "type_traits/enable_if.hpp"
910
#include "type_traits/function_traits.hpp"
1011
#include "type_traits/integral_constant.hpp"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright © 2014-2024, Benoit BLANCHON
3+
// MIT License
4+
5+
#pragma once
6+
7+
#include <stddef.h> // size_t
8+
9+
#include <ArduinoJson/Namespace.hpp>
10+
11+
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
12+
13+
template <typename T>
14+
struct decay {
15+
using type = T;
16+
};
17+
18+
template <typename T>
19+
struct decay<T&> : decay<T> {};
20+
21+
template <typename T>
22+
struct decay<T&&> : decay<T> {};
23+
24+
template <typename T>
25+
struct decay<T[]> : decay<T*> {};
26+
27+
template <typename T, size_t N>
28+
struct decay<T[N]> : decay<T*> {};
29+
30+
template <typename T>
31+
using decay_t = typename decay<T>::type;
32+
33+
ARDUINOJSON_END_PRIVATE_NAMESPACE

0 commit comments

Comments
 (0)