File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,10 @@ class Span final {
5555 template <size_t N>
5656 /* implicit */ constexpr Span (T (&Arr)[N]) : data_(Arr), length_(N) {}
5757
58+ // / Construct a Span from a single element reference.
59+ /* implicit */ constexpr Span (T& single_element)
60+ : data_(&single_element), length_(1 ) {}
61+
5862 // / @returns a pointer to the start of the underlying element buffer.
5963 iterator begin () const noexcept {
6064 return data_;
Original file line number Diff line number Diff line change @@ -61,3 +61,19 @@ TEST(SpanTest, TriviallyCopyable) {
6161 EXPECT_EQ (span.size (), span_copy.size ());
6262 EXPECT_TRUE (std::is_trivially_copyable<Span<int64_t >>::value);
6363}
64+
65+ TEST (SpanTest, SingleElementConstructor) {
66+ int64_t single_value = 42 ;
67+ Span<int64_t > span = single_value;
68+
69+ EXPECT_EQ (span.size (), 1 );
70+ EXPECT_EQ (span.data (), &single_value);
71+ EXPECT_EQ (span[0 ], 42 );
72+ EXPECT_EQ (*span.begin (), 42 );
73+ EXPECT_EQ (span.end (), span.begin () + 1 );
74+
75+ // Test that modifying through span affects original value
76+ span[0 ] = 100 ;
77+ EXPECT_EQ (single_value, 100 );
78+ EXPECT_EQ (span[0 ], 100 );
79+ }
You can’t perform that action at this time.
0 commit comments