namespace std { inline namespace literals { inline namespace string_view_literals { constexpr std::string_view operator""sv(const char* str, std::size_t len) noexcept; constexpr std::u16string_view operator""sv(const char16_t* str, std::size_t len) noexcept; constexpr std::u32string_view operator""sv(const char32_t* str, std::size_t len) noexcept; constexpr std::wstring_view operator""sv(const wchar_t* str, std::size_t len) noexcept; }} }
概要
basic_string_view
型のリテラル。
文字列リテラルを受け取り、各文字型のbasic_string_view
オブジェクトを構築する。
- (1) :
string_view
型のリテラル - (2) :
u16string_view
型のリテラル - (3) :
u32string_view
型のリテラル - (4) :
wstring_view
型のリテラル
戻り値
- (1) :
string_view{str, len}
- (2) :
u16string_view{str, len}
- (3) :
u32string_view{str, len}
- (4) :
wstring_view{str, len}
備考
- 中間にヌル文字を含む文字列リテラルから
basic_string_view
オブジェクトを構築する場合、コンストラクタを使用するよりもこちらの関数を使用したほうがよい。const char*
をとるコンストラクタはstd::char_traits::length()
関数を使用して文字列長を計算するため、ヌル終端となってしまう- こちらの関数は文字列リテラルの長さを直接扱うため、文字列全体を参照する
basic_string_view
オブジェクトを構築できる
例
#include <iostream> #include <string_view> #include <type_traits> int main() { using namespace std::string_view_literals; auto sv1 = "Hello"sv; // sv1の型はstd::string_view auto sv2 = u"Hello"sv; // sv2の型はstd::u16string_view auto sv3 = U"Hello"sv; // sv3の型はstd::u32string_view auto sv4 = L"Hello"sv; // sv4の型はstd::wstring_view std::cout << sv1.substr(0, 3) << std::endl; static_assert(std::is_same<decltype(sv1), std::string_view>{}); static_assert(std::is_same<decltype(sv2), std::u16string_view>{}); static_assert(std::is_same<decltype(sv3), std::u32string_view>{}); static_assert(std::is_same<decltype(sv4), std::wstring_view>{}); }
出力
Hel
バージョン
言語
- C++17
処理系
- Clang, C++17 mode: 4.0
- GCC, C++17 mode: 7.1
- ICC: ??
- Visual C++: ??