std::string_view constructors
std::basic_string_view
class can be constructed in many different ways.
Use the button in the top-right corner to navigate with arrows for convenience.
std::basic_string_view
is a class template, with following type parameters,
that are used within constructors:
pub | CharT | Character type |
pub | Traits |
important Like for |
Default constructor
- C++20
- Simplified
- Detailed
basic_string_view();
- this constructor is
constexpr
andnoexcept
constexpr basic_string_view() noexcept;
Default constructor. Constructs an empty string view with:
data() == nullptr
size() == 0
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
// Default constructed string view
std::string_view sv;
std::cout << sv.size() << ' ' << std::quoted(sv);
}
0 ""
Defaulted copy constructor
- C++20
constexpr basic_string_view( const basic_string_view& other ) noexcept
= default;
Copy constructor. Constructs a view of the same content as other
, which has:
data() == other.data()
size() == other.size()
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
std::string_view sv = "Hello";
std::string_view sv2 = sv;
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
std::cout << sv2.size() << ' ' << std::quoted(sv2);
}
5 "Hello"
5 "Hello"
Char pointer constructor (bound)
- C++20
constexpr basic_string_view( const CharT* s, size_type count );
Constructs a view of the first count characters of the character array starting with the element pointed by s
. The created view has:
data() == s
size() == count
The behavior is undefined
if:- [ s, s + count ) is not a valid range (even though the constructor may not access any of the elements of this range)
s
can contain null characters.
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
std::string_view sv("Hello World!", 5);
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
}
5 "Hello"
Char pointer constructor (unbound)
- C++20
constexpr basic_string_view( const CharT* s );
data() == s
size() == Traits::length(s)
The behavior is undefined
if:- [ s, s + Traits::length(s) ) is not a valid range (even though the constructor may not access any of the elements of this range)
The length of the view is determined as if by Traits::length(s)
.
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
const char* string = "Hello\0 World!";
std::string_view sv(string);
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
}
5 "Hello"
Copy range constructor (iterators)
- C++20
template< class It, class End >
constexpr basic_string_view( It first, End last );
Constructs a string view over the range [ first, last ). The resulting view has:
data() == std::to_address(first)
size() == last - first
The behavior is undefined
if:- **[ first, last ) is not a valid range
It
does not modelcontiguous_iterator
orEnd
does not modelsized_sentinel_for
Overload resolution
These overloads participate in overload resolution only if:
It
satisfiescontiguous_iterator
End
satisfiessized_sentinel_for
forIt
std::iter_value_t<It>
andCharT
are the same type
andEnd
is not convertible tostd::size_t
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
int main() {
std::string s = "Hello World!";
std::string_view sv(s.begin(), s.begin()+5);
std::string_view sv2(s.begin() + 6, s.end());
std::cout << sv << ' ' << sv2;
}
Hello World!
Copy range constructor (range)
- C++23
template< class R >
explicit constexpr basic_string_view( R&& r );
Constructs a string view over the range r
.
The resulting view has:
data() == ranges::data(r)
size() == ranges::size(r)
Overload resolution
These overloads participate in overload resolution only if:
std::remove_cvref_t<R>
is not the same type asstd::basic_string_view
R
modelscontiguous_range
andsized_range
ranges::range_value_t<R>
andCharT
are the same typeR
is not convertible toconst CharT*
- let
d
be an lvalue of typestd::remove_cvref_t<R>
,d.operator ::std::basic_string_view<CharT, Traits>()
is not a valid expression
and - if qualified-id
std::remove_reference_t<R>::traits_type
is valid and denotes a type, it is same asTraits
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
int main() {
std::string s = "Hello";
std::string_view sv(s);
std::cout << sv;
}
Hello
Deleted std::nullptr_t
constructor
- C++23
constexpr basic_string_view( std::nullptr_t ) = delete;
A deleted constructor that prohibits constructing a string view from a nullptr
.
Example
#include <string_view>
int main() {
// std::string_view sv(nullptr); // Won't compile!
}
Hover to see the original license.