GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::string_literals::sリテラル - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. sリテラル

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::string_literals::sリテラル(C++14)

namespace std {
inline namespace literals {
inline namespace string_literals {
  string operator""s(const char* str, size_t len);                  // (1) C++14
  constexpr string operator""s(const char* str, size_t len);        // (1) C++20

  constexpr u8string operator""s(const char8_t* str, size_t len);   // (2) C++20

  u16string operator""s(const char16_t* str, size_t len);           // (3) C++14
  constexpr u16string operator""s(const char16_t* str, size_t len); // (3) C++20

  u32string operator""s(const char32_t* str, size_t len);           // (4) C++14
  constexpr u32string operator""s(const char32_t* str, size_t len); // (4) C++20

  wstring operator""s(const wchar_t* str, size_t len);              // (5) C++14
  constexpr wstring operator""s(const wchar_t* str, size_t len);    // (5) C++20
}}}

概要

basic_string型のリテラル。

文字列リテラルを受け取り、各文字型のbasic_stringオブジェクトを構築する。

  • (1) : string型のリテラル
  • (2) : u8string型のリテラル
  • (3) : u16string型のリテラル
  • (4) : u32string型のリテラル
  • (5) : wstring型のリテラル

戻り値

  • (1) : basic_string<char>{str, len}
  • (2) : basic_string<char8_t>{str, len}
  • (3) : basic_string<char16_t>{str, len}
  • (4) : basic_string<char32_t>{str, len}
  • (5) : basic_string<wchar_t>{str, len}

例

#include <string>

int main()
{
  using namespace std::literals::string_literals;

  std::string s1 = "hello"s;   // 文字コード未規定のstringリテラル
#if defined(__cpp_char8_t) && 201803L <= __cpp_char8_t
  std::u8string s2 = u8"hello"s; // UTF-8のstringリテラル(C++20)
#else
  std::string s2 = u8"hello"s; // UTF-8のstringリテラル(C++11~C++17)
#endif
  std::u16string s3 = u"hello"s; // u16stringリテラル
  std::u32string s4 = U"hello"s; // u32stringリテラル

  std::wstring s5 = L"hello"s; // 文字コード未規定のwstringリテラル
}

出力

バージョン

言語

  • C++14

処理系

  • Clang: 3.4 ✅
  • GCC: 4.9.0 ✅
  • Visual C++: 2015 ✅

参照

  • N3642 User-defined Literals for Standard Library Types (part 1 - version 4)
  • P0980R1 Making std::string constexpr
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.