Skip to content

Commit 25fa507

Browse files
committed
Added sfl::dtl::is_trivially_copyable
1 parent a93f25c commit 25fa507

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

include/sfl/detail/bit/bit_cast.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#ifndef SFL_DETAIL_BIT_CAST_HPP_INCLUDED
2222
#define SFL_DETAIL_BIT_CAST_HPP_INCLUDED
2323

24+
#include <sfl/detail/type_traits/is_trivially_copyable.hpp>
2425
#include <sfl/detail/cpp.hpp>
2526

2627
#include <cstring> // memcpy
27-
#include <type_traits> // is_trivially_copyable
2828

2929
#if SFL_CPP_VERSION >= SFL_CPP_20
3030
#include <bit> // bit_cast
@@ -46,13 +46,8 @@ template <class To, class From>
4646
To bit_cast_impl_11(const From& src) noexcept
4747
{
4848
static_assert(sizeof(To) == sizeof(From), "To and From must have the same size");
49-
#if (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 5)) || (defined(__clang__) && (__clang_major__ < 4))
50-
static_assert(__has_trivial_copy(From), "From must be trivially copyable");
51-
static_assert(__has_trivial_copy(To), "To must be trivially copyable");
52-
#else
53-
static_assert(std::is_trivially_copyable<From>::value, "From must be trivially copyable");
54-
static_assert(std::is_trivially_copyable<To>::value, "To must be trivially copyable");
55-
#endif
49+
static_assert(sfl::dtl::is_trivially_copyable<From>::value, "From must be trivially copyable");
50+
static_assert(sfl::dtl::is_trivially_copyable<To>::value, "To must be trivially copyable");
5651

5752
To dst;
5853
std::memcpy(&dst, &src, sizeof(To));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright (c) 2022 Slaven Falandys
3+
//
4+
// This software is provided 'as-is', without any express or implied
5+
// warranty. In no event will the authors be held liable for any damages
6+
// arising from the use of this software.
7+
//
8+
// Permission is granted to anyone to use this software for any purpose,
9+
// including commercial applications, and to alter it and redistribute it
10+
// freely, subject to the following restrictions:
11+
//
12+
// 1. The origin of this software must not be misrepresented; you must not
13+
// claim that you wrote the original software. If you use this software
14+
// in a product, an acknowledgment in the product documentation would be
15+
// appreciated but is not required.
16+
// 2. Altered source versions must be plainly marked as such, and must not be
17+
// misrepresented as being the original software.
18+
// 3. This notice may not be removed or altered from any source distribution.
19+
//
20+
21+
#ifndef SFL_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
22+
#define SFL_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED
23+
24+
#include <type_traits> // is_trivially_copyable, integral_constant
25+
26+
namespace sfl
27+
{
28+
29+
namespace dtl
30+
{
31+
32+
template <typename T>
33+
struct is_trivially_copyable
34+
#if (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 5)) || (defined(__clang__) && (__clang_major__ < 4))
35+
: std::integral_constant<bool, __has_trivial_copy(T)>
36+
#else
37+
: std::is_trivially_copyable<T>
38+
#endif
39+
{};
40+
41+
} // namespace dtl
42+
43+
} // namespace sfl
44+
45+
#endif // SFL_DETAIL_IS_TRIVIALLY_COPYABLE_HPP_INCLUDED

test/src/normal_iterator/normal_iterator.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@
22

33
#include "sfl/detail/normal_iterator.hpp"
44

5+
#include "sfl/detail/type_traits/is_trivially_copyable.hpp"
6+
57
#include <type_traits>
68

79
using iterator = sfl::dtl::normal_iterator<int*, false>;
810

911
using const_iterator = sfl::dtl::normal_iterator<int*, true>;
1012

11-
#if (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 5)) || (defined(__clang__) && (__clang_major__ < 4))
12-
13-
static_assert(__has_trivial_copy(iterator), "");
14-
15-
static_assert(__has_trivial_copy(const_iterator), "");
16-
17-
#else
18-
19-
static_assert(std::is_trivially_copyable<iterator>::value, "");
20-
21-
static_assert(std::is_trivially_copyable<const_iterator>::value, "");
13+
static_assert(sfl::dtl::is_trivially_copyable<iterator>::value, "");
2214

23-
#endif
15+
static_assert(sfl::dtl::is_trivially_copyable<const_iterator>::value, "");
2416

2517
static_assert(std::is_same<typename iterator::pointer, int*>::value, "");
2618

test/src/static_storage/static_storage.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "sfl/detail/static_storage.hpp"
44

5+
#include "sfl/detail/type_traits/is_trivially_copyable.hpp"
6+
57
#include "check.hpp"
68

79
#include <iterator>
@@ -14,19 +16,9 @@ using pointer = typename static_storage::pointer;
1416

1517
using const_pointer = typename static_storage::const_pointer;
1618

17-
#if (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 5)) || (defined(__clang__) && (__clang_major__ < 4))
18-
19-
static_assert(__has_trivial_copy(pointer), "");
20-
21-
static_assert(__has_trivial_copy(const_pointer), "");
22-
23-
#else
24-
25-
static_assert(std::is_trivially_copyable<pointer>::value, "");
26-
27-
static_assert(std::is_trivially_copyable<const_pointer>::value, "");
19+
static_assert(sfl::dtl::is_trivially_copyable<pointer>::value, "");
2820

29-
#endif
21+
static_assert(sfl::dtl::is_trivially_copyable<const_pointer>::value, "");
3022

3123
static_assert(std::is_same<typename std::pointer_traits<pointer>::element_type, int>::value, "");
3224

0 commit comments

Comments
 (0)