Skip to content

Commit

Permalink
Ensure that span and string_view are trivially copyable (#4957)
Browse files Browse the repository at this point in the history
* Ensure that span and string_view are trivially copyable

* Remove unused variable
  • Loading branch information
Marshall Clow authored and GitHub Enterprise committed Oct 14, 2024
1 parent 4317b2e commit 13efe37
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
48 changes: 10 additions & 38 deletions groups/bsl/bslstl/bslstl_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ class span {
static const size_type extent = EXTENT;
// BDE_VERIFY pragma: pop

// TRAITS
BSLMF_NESTED_TRAIT_DECLARATION(span, bsl::is_trivially_copyable);

// CREATORS

/// Construct an empty `span` object. The behavior is undefined unless
Expand All @@ -318,8 +321,7 @@ class span {

/// Create a span that refers to the same data as the specified
/// `original` object.
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span& original)
BSLS_KEYWORD_NOEXCEPT;
//! BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span&) noexcept = default;

/// Construct a span that refers to the specified `count` consecutive
/// objects starting from the specified `ptr`. The behavior is
Expand Down Expand Up @@ -550,8 +552,7 @@ class span {

/// Assign to this span the value of the specified `rhs` object, and
/// return a reference providing modifiable access to this span.
BSLS_KEYWORD_CONSTEXPR_CPP14
span& operator=(const span&) BSLS_KEYWORD_NOEXCEPT;
//! constexpr span& operator=(const span&) noexcept = default;

/// Exchange the value of this span with the value of the specified
/// `other` object.
Expand Down Expand Up @@ -584,15 +585,17 @@ class span<TYPE, dynamic_extent> {
static const size_type extent = dynamic_extent;
// BDE_VERIFY pragma: pop

// TRAITS
BSLMF_NESTED_TRAIT_DECLARATION(span, bsl::is_trivially_copyable);

// CREATORS

/// Construct an empty `span` object.
BSLS_KEYWORD_CONSTEXPR_CPP14 span() BSLS_KEYWORD_NOEXCEPT;

/// Create a span that refers to the same data as the specified
/// `original` object.
BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span& original)
BSLS_KEYWORD_NOEXCEPT;
//! BSLS_KEYWORD_CONSTEXPR_CPP14 span(const span&) noexcept = default;

/// Construct a span that refers to the specified `count` consecutive
/// objects starting from the specified `ptr`.
Expand Down Expand Up @@ -844,13 +847,7 @@ class span<TYPE, dynamic_extent> {

/// Assign to this span the value of the specified `rhs` object, and
/// return a reference providing modifiable access to this span.
BSLS_KEYWORD_CONSTEXPR_CPP14
span& operator=(const span& rhs) BSLS_KEYWORD_NOEXCEPT
{
d_data_p = rhs.d_data_p;
d_size = rhs.d_size;
return *this;
}
//! constexpr span& operator=(const span&) noexcept = default;

/// Exchange the value of this span with the value of the specified
/// `other` object.
Expand Down Expand Up @@ -962,13 +959,6 @@ bsl::span<TYPE, EXTENT>::span() BSLS_KEYWORD_NOEXCEPT
BSLMF_ASSERT(EXTENT == 0);
}

template <class TYPE, size_t EXTENT>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
bsl::span<TYPE, EXTENT>::span(const span &original) BSLS_KEYWORD_NOEXCEPT
: d_data_p(original.d_data_p)
{
}

template <class TYPE, size_t EXTENT>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
bsl::span<TYPE, EXTENT>::span(pointer ptr, size_type count)
Expand Down Expand Up @@ -1188,15 +1178,6 @@ bsl::span<TYPE, EXTENT>::rend() const BSLS_KEYWORD_NOEXCEPT
}

// MANIPULATORS
template <class TYPE, size_t EXTENT>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
bsl::span<TYPE, EXTENT>&
bsl::span<TYPE, EXTENT>::operator=(const span &rhs) BSLS_KEYWORD_NOEXCEPT
{
d_data_p = rhs.d_data_p;
return *this;
}

template <class TYPE, size_t EXTENT>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
void bsl::span<TYPE, EXTENT>::swap(span &other) BSLS_KEYWORD_NOEXCEPT
Expand All @@ -1219,15 +1200,6 @@ bsl::span<TYPE, bsl::dynamic_extent>::span() BSLS_KEYWORD_NOEXCEPT
{
}

template <class TYPE>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
bsl::span<TYPE, bsl::dynamic_extent>::span(const span& original)
BSLS_KEYWORD_NOEXCEPT
: d_data_p(original.d_data_p)
, d_size(original.d_size)
{
}

template <class TYPE>
BSLS_KEYWORD_CONSTEXPR_CPP14 inline
bsl::span<TYPE, bsl::dynamic_extent>::span(pointer ptr, size_type count)
Expand Down
33 changes: 33 additions & 0 deletions groups/bsl/bslstl/bslstl_span.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void aSsErT(bool condition, const char *message, int line)
#define ASSERT_NOT_NOEXCEPT(...) BSLS_ASSERT(true)
#endif

using namespace BloombergLP;

//=============================================================================
// USAGE EXAMPLE
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -319,6 +321,27 @@ void TestBasicConstructors()
ASSERT(&arr[5] == psD2b.data());
ASSERT(5 == psD2b.size());
}

// constexpr copy constructor and assignment
#ifdef BSLS_COMPILERFEATURES_SUPPORT_CONSTEXPR_CPP14
{
constexpr static int val1 = 5;

constexpr bsl::span<const int, 1> psS(&val1, 1);
constexpr bsl::span<const int> psD(&val1, 1);
constexpr bsl::span<const int, 1> psS1(psS);
constexpr bsl::span<const int> psD1(psD);

static_assert(1 == psS1.size());
static_assert(&val1 == psS1.data());
static_assert(5 == psS1[0]);

static_assert(1 == psD1.size());
static_assert(&val1 == psD1.data());
static_assert(5 == psD1[0]);
}
#endif

}


Expand Down Expand Up @@ -1464,6 +1487,16 @@ int main(int argc, char *argv[])
ASSERT(7 == ss0[2]);

ASSERT(bsl::dynamic_extent == size_t(-1));

ASSERT((bsl::is_trivially_copyable<bsl::span<int> >::value));
ASSERT((bsl::is_trivially_copyable<bsl::span<int, 6> >::value));
ASSERT((bsl::is_trivially_copyable<bsl::span<bsl::string> >::value));
ASSERT((bsl::is_trivially_copyable<bsl::span<bsl::string, 6> >::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::span<int> >::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::span<int, 6> >::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::span<bsl::string> >::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::span<bsl::string, 6> >::value));

} break;
default: {
fprintf(stderr, "WARNING: CASE `%d' NOT FOUND.\n", test);
Expand Down
32 changes: 29 additions & 3 deletions groups/bsl/bslstl/bslstl_stringview.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <bslma_testallocatormonitor.h>

#include <bslmf_isbitwisecopyable.h>
#include <bslmf_istriviallycopyable.h>

#include <bsls_assert.h>
#include <bsls_asserttest.h>
Expand Down Expand Up @@ -7396,9 +7397,34 @@ int main(int argc, char *argv[])
if (verbose) printf("\nTESTING TYPE TRAITS"
"\n===================\n");

ASSERT((bsl::is_trivially_copyable<bsl::string_view>::value));
ASSERT((bslmf::IsBitwiseCopyable<bsl::string_view>::value));
ASSERT((bslmf::IsBitwiseMoveable<bsl::string_view>::value));
ASSERT((bsl::is_trivially_copyable< bsl::string_view>::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::string_view>::value));
ASSERT((bslmf::IsBitwiseCopyable< bsl::string_view>::value));
ASSERT((bslmf::IsBitwiseMoveable< bsl::string_view>::value));

ASSERT((bsl::is_trivially_copyable< bsl::wstring_view>::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::wstring_view>::value));
ASSERT((bslmf::IsBitwiseCopyable< bsl::wstring_view>::value));
ASSERT((bslmf::IsBitwiseMoveable< bsl::wstring_view>::value));

#if defined (BSLS_COMPILERFEATURES_SUPPORT_UTF8_CHAR_TYPE)
ASSERT((bsl::is_trivially_copyable< bsl::u8string_view>::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::u8string_view>::value));
ASSERT((bslmf::IsBitwiseCopyable< bsl::u8string_view>::value));
ASSERT((bslmf::IsBitwiseMoveable< bsl::u8string_view>::value));
#endif

#if defined(BSLS_COMPILERFEATURES_SUPPORT_UNICODE_CHAR_TYPES)
ASSERT((bsl::is_trivially_copyable< bsl::u16string_view>::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::u16string_view>::value));
ASSERT((bslmf::IsBitwiseCopyable< bsl::u16string_view>::value));
ASSERT((bslmf::IsBitwiseMoveable< bsl::u16string_view>::value));

ASSERT((bsl::is_trivially_copyable< bsl::u32string_view>::value));
ASSERT((bslmf::IsTriviallyCopyableCheck<bsl::u32string_view>::value));
ASSERT((bslmf::IsBitwiseCopyable< bsl::u32string_view>::value));
ASSERT((bslmf::IsBitwiseMoveable< bsl::u32string_view>::value));
#endif

} break;
case 23: {
Expand Down

0 comments on commit 13efe37

Please sign in to comment.