Namespaces
Variants

std::filesystem::path::generic_string, std::filesystem::path::generic_wstring, std::filesystem::path::generic_u8string, std::filesystem::path::generic_u16string, std::filesystem::path::generic_u32string

ja.cppreference.net から
 
 
 
 
template< class CharT, class Traits = std::char_traits<CharT>,
          class Alloc = std::allocator<CharT> >
std::basic_string<CharT,Traits,Alloc>
    generic_string( const Alloc& a = Alloc() ) const;
(1) (C++17 以上)
std::string generic_string() const;
(2) (C++17 以上)
std::wstring generic_wstring() const;
(3) (C++17 以上)
std::u16string generic_u16string() const;
(4) (C++17 以上)
std::u32string generic_u32string() const;
(5) (C++17 以上)
std::string generic_u8string() const;
(6) (C++17 以上)
(C++20 まで)
std::u8string generic_u8string() const;
(C++20 以上)

ジェネリックパス名形式の内部パス名を、指定された文字列型に変換して返します。変換がある場合、その仕様は以下の通りです。

  • もし path::value_type が char の場合、変換があればシステム依存です。これはネイティブエンコーディングが UTF-8 であり、string() が変換を行わない典型的な POSIX システム(Linux など)の場合です。
  • そうでなければ、path::value_type が wchar_t の場合、変換があれば未規定です。これは wchar_t が 16 ビットでネイティブエンコーディングが UTF-16 である Windows の場合です。
  • そうでなければ、path::value_type が char16_t の場合、ネイティブエンコーディングは UTF-16 で変換方法は未規定です。
  • そうでなければ、path::value_type が char32_t の場合、ネイティブエンコーディングは UTF-32 で変換方法は未規定です。
  • そうでなければ、path::value_type が char8_t の場合、ネイティブエンコーディングは UTF-8 で変換方法は未規定です。

ディレクトリ区切り文字として / 文字が使用されます。

1) メモリ割り当てはすべて a によって行われます。
6) u8string() の場合の結果のエンコーディングは常に UTF-8 です。

パラメータ

a - 文字列の構築に使用するアロケータ
型要件
-
CharT はエンコードされた文字型のいずれかでなければならない ( char , wchar_t , char8_t (C++20以降) , char16_t および char32_t )。

戻り値

内部パス名を汎用パス名形式で、指定された文字列型に変換したもの。

例外

実装定義の例外をスローする可能性があります。

例

#include <cstddef>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <span>
#include <string_view>
void print(std::string_view rem, auto const& str)
{
    std::cout << rem << std::hex << std::uppercase << std::setfill('0');
    for (const auto b : std::as_bytes(std::span{str}))
        std::cout << std::setw(2) << std::to_integer<unsigned>(b) << ' ';
    std::cout << '\n';
}
int main()
{
    std::filesystem::path p{"/家/屋"};
    std::cout << p << '\n';
    print("string    : ", p.generic_string());
    print("u8string  : ", p.generic_u8string());
    print("u16string : ", p.generic_u16string());
    print("u32string : ", p.generic_u32string());
    print("wstring   : ", p.generic_wstring());
}

出力例:

"/家/屋"
string    : 2F E5 AE B6 2F E5 B1 8B
u8string  : 2F E5 AE B6 2F E5 B1 8B
u16string : 2F 00 B6 5B 2F 00 4B 5C
u32string : 2F 00 00 00 B6 5B 00 00 2F 00 00 00 4B 5C 00 00
wstring   : 2F 00 00 00 B6 5B 00 00 2F 00 00 00 4B 5C 00 00

関連項目

ネイティブのパス名形式に変換されたパスを文字列として返す
(公開メンバ関数)