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 {
55
55
template <size_t N>
56
56
/* implicit */ constexpr Span (T (&Arr)[N]) : data_(Arr), length_(N) {}
57
57
58
+ // / Construct a Span from a single element reference.
59
+ /* implicit */ constexpr Span (T& single_element)
60
+ : data_(&single_element), length_(1 ) {}
61
+
58
62
// / @returns a pointer to the start of the underlying element buffer.
59
63
iterator begin () const noexcept {
60
64
return data_;
Original file line number Diff line number Diff line change @@ -61,3 +61,19 @@ TEST(SpanTest, TriviallyCopyable) {
61
61
EXPECT_EQ (span.size (), span_copy.size ());
62
62
EXPECT_TRUE (std::is_trivially_copyable<Span<int64_t >>::value);
63
63
}
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