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

From 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以降)
(2) (C++17以降)
std:: string generic_string ( ) const ;
std:: wstring generic_wstring ( ) const ;
std:: u16string generic_u16string ( ) const ;
std:: u32string generic_u32string ( ) const ;
(3)
std:: string generic_u8string ( ) const ;
(C++17以降)
(C++20まで)
std:: u8string generic_u8string ( ) const ;
(C++20以降)

内部パス名を汎用パス名形式で返し、特定の文字列型に変換します。変換が必要な場合、以下のように指定されます:

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

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

1) すべてのメモリ割り当ては a によって実行されます。
3) 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

関連項目

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