Przejdź do głównej zawartości

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.

zanotuj

std::basic_string_view is a class template, with following type parameters, that are used within constructors:

pubCharTCharacter type
pubTraits

CharTraits class specifying the operations on the character type.

important

Like for std::basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed

.

Default constructor

basic_string_view();
  • this constructor is constexpr and 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);
}
Result (console)
0 ""

Defaulted copy constructor

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);
}
Result (console)
5 "Hello"
5 "Hello"

Char pointer constructor (bound)

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
Undefined Behavior

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)
zanotuj

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';
}
Result (console)
5 "Hello"

Char pointer constructor (unbound)

constexpr basic_string_view( const CharT* s );
  • data() == s
  • size() == Traits::length(s)
Undefined Behavior

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)
zanotuj

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';
}
Result (console)
5 "Hello"

Copy range constructor (iterators)

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
Undefined Behavior

The behavior is undefined

if:

Overload resolution

These overloads participate in overload resolution only if:

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;
}
Result (console)
Hello World!

Copy range constructor (range)

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 as std::basic_string_view
  • R models contiguous_range and sized_range
  • ranges::range_value_t<R> and CharT are the same type
  • R is not convertible to const CharT*
  • let d be an lvalue of type std::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 as Traits

Complexity

Constant - O(1).

Example

#include <string_view>
#include <iostream>

int main() {
std::string s = "Hello";
std::string_view sv(s);

std::cout << sv;
}
Result (console)
Hello

Deleted std::nullptr_t constructor

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!
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

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.

zanotuj

std::basic_string_view is a class template, with following type parameters, that are used within constructors:

pubCharTCharacter type
pubTraits

CharTraits class specifying the operations on the character type.

important

Like for std::basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed

.

Default constructor

basic_string_view();
  • this constructor is constexpr and 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);
}
Result (console)
0 ""

Defaulted copy constructor

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);
}
Result (console)
5 "Hello"
5 "Hello"

Char pointer constructor (bound)

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
Undefined Behavior

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)
zanotuj

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';
}
Result (console)
5 "Hello"

Char pointer constructor (unbound)

constexpr basic_string_view( const CharT* s );
  • data() == s
  • size() == Traits::length(s)
Undefined Behavior

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)
zanotuj

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';
}
Result (console)
5 "Hello"

Copy range constructor (iterators)

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
Undefined Behavior

The behavior is undefined

if:

Overload resolution

These overloads participate in overload resolution only if:

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;
}
Result (console)
Hello World!

Copy range constructor (range)

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 as std::basic_string_view
  • R models contiguous_range and sized_range
  • ranges::range_value_t<R> and CharT are the same type
  • R is not convertible to const CharT*
  • let d be an lvalue of type std::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 as Traits

Complexity

Constant - O(1).

Example

#include <string_view>
#include <iostream>

int main() {
std::string s = "Hello";
std::string_view sv(s);

std::cout << sv;
}
Result (console)
Hello

Deleted std::nullptr_t constructor

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!
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.