forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssize.h
44 lines (32 loc) · 1.27 KB
/
ssize.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
// Many C++20 std headers define ssize; pick one. This is needed even to
// get the __cpp_lib_ssize define set.
#include <vector>
#ifdef __cpp_lib_ssize
namespace drake {
using std::ssize;
} // namespace drake
#else // __cpp_lib_ssize
#include <cstddef> // For std::ptrdiff_t
#include <type_traits> // For std::common_type_t, std::make_signed_t
namespace drake {
/** Implements C++20 %std::ssize() for earlier compilers. See
https://en.cppreference.com/w/cpp/iterator/size for documentation. Will be
removed once all Drake-supported platforms offer %std::ssize(). */
// The implementations below are taken directly from the cppreference
// documentation. BTW std::size() is already supported on all Drake platforms.
template <class C>
constexpr auto ssize(const C& c)
-> std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())> > {
using R = std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())> >;
return static_cast<R>(c.size());
}
/** This signature returns the size of built-in (C style) arrays. */
template <class T, std::ptrdiff_t N>
constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept {
return N;
}
} // namespace drake
#endif // __cpp_lib_ssize