std::filesystem::u8path
提供: cppreference.com
< cpp | filesystem | path
ヘッダ <filesystem> で定義
|
||
template< class Source > path u8path( const Source& source ); |
(1) | (C++17以上) (C++20で非推奨) |
template< class InputIt > path u8path( InputIt first, InputIt last ); |
(2) | (C++17以上) (C++20で非推奨) |
std::string、std::string_view、ヌル終端マルチバイト文字列、またはイテレータの組 [first, last) として提供される UTF-8 エンコードされた char シー��ンスからパスを構築します。
-
path::value_type
が char でネイティブエンコーディングが UTF-8 の場合、 path(source) または path(first, last) で行われたかのように、パスを直接構築します。 ノート: これは Linux のような Unicode を使用する POSIX システムの一般的な状況です。 -
path::value_type
が wchar_t でネイティブエンコーディングが UTF-16 (これは Windows の状況です)、またはpath::value_type
が char16_t (ネイティブエンコーディングが UTF-16 であることが保証されます) または char32_t (ネイティブエンコーディングが UTF-32 であることが保証されます) の場合、まずその UTF-8 文字シーケンスがpath::string_type
型の一時的な文字列tmp
に変換され、それから path(tmp) によって行われたかのように新しいパスが構築されます。 - そうでなければ (UTF-8 でないナロー文字エンコーディングの場合と UTF-16 でない wchar_t の場合)、まずその UTF-8 文字シーケンスが std::u32string 型の UTF-32 エンコードされた一時的な文字列
tmp
に変換され、それから path(tmp) によって行われたかのように新しいパスが構築されます (Unicode でないマルチバイトまたはシングルバイトエンコードされたファイルシステムを持つ POSIX システムでは、この経路が取られます)。
目次 |
[編集] 引数
source | - | UTF-8 エンコードされた std::string、std::string_view、ヌル終端文字列を指すポインタ、またはヌル終端文字列を指す文字値型の入力イテレータ |
first, last | - | UTF-8 エンコードされた文字シーケンスを表す一組の LegacyInputIterator |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-InputIt の値型は char でなければなりません。
|
[編集] 戻り値
UTF-8 からファイルシステムのネイティブ文字エンコーディングに変換された後の入力文字列から構築��れたパス。
[編集] 例外
メモリ確保に失敗した場合は std::bad_alloc を投げる可能性があります。
[編集] ノート
ネイティブパス形式が汎用パス形式と異なるシステム (Windows や POSIX システムはいずれもそういった OS の例ではありません) では、この関数の引数に汎用形式を使用すると、ネイティブ形式に変換されます。
[編集] 例
Run this code
#include <cstdio> #ifdef _MSC_VER #include <io.h> #include <fcntl.h> #else #include <locale> #include <clocale> #endif #include <fstream> #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { #ifdef _MSC_VER _setmode(_fileno(stderr), _O_WTEXT); #else std::setlocale(LC_ALL, ""); std::locale::global(std::locale("")); std::cout.imbue(std::locale()); std::wcerr.imbue(std::locale()); #endif fs::path p = fs::u8path(u8"要らない.txt"); std::ofstream(p) << "File contents"; // LWG2676 の前では、 string_type が wstring // である MSVC においては、 operator string_type() // の使用は非標準の拡張によってのみ動作します。 // LWG2676 の後では、 fstream の新しいコンストラクタ // が使用されます。 // ネイティブ文字列表現は OS の API で使用できます。 if (std::FILE* f = #ifdef _MSC_VER _wfopen(p.c_str(), L"r") #else std::fopen(p.c_str(), "r") #endif ) { int ch; while((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // マルチバイト表現とワイド表現は出力のために使用できます。 std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; std::wcerr << "File name in wide encoding: " << p.wstring() << '\n'; fs::remove(p); }
出力:
File contents File name in narrow multibyte encoding: 要らない.txt File name in wide encoding: 要らない.txt
[編集] 関連項目
(C++17) |
パスを表します (クラス) |