“cpp/filesystem/path/u8path”的版本间的差异
来自cppreference.com
< cpp | filesystem | path
小 |
小 |
||
(未显示1个用户的1个中间版本) | |||
第2行: | 第2行: | ||
{{cpp/filesystem/path/navbar}} | {{cpp/filesystem/path/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | filesystem}} | + | {{dcl header|filesystem}} |
− | {{dcl | num=1 | since=c++17 | deprecated=c++20 | | + | {{dcl|num=1|since=c++17|deprecated=c++20| |
template< class Source > | template< class Source > | ||
std::filesystem::path u8path( const Source& source ); | std::filesystem::path u8path( const Source& source ); | ||
}} | }} | ||
− | {{dcl | num=2 | since=c++17 | deprecated=c++20 | | + | {{dcl|num=2|since=c++17|deprecated=c++20| |
template< class InputIt > | template< class InputIt > | ||
std::filesystem::path u8path( InputIt first, InputIt last ); | std::filesystem::path u8path( InputIt first, InputIt last ); | ||
第13行: | 第13行: | ||
{{dcl end}} | {{dcl end}} | ||
− | 从 UTF-8 编码的 {{c|char}}{{rev inl|since=c++20| 或 {{c|char8_t}}}} 序列构造 | + | 从 UTF-8 编码的 {{c|char}}{{rev inl|since=c++20| 或 {{c|char8_t}}}} 序列构造 {{tt|p}},源作为 {{lc|std::string}} 或 {{lc|std::string_view}},或空终止多字节字符串,或作为一对迭代器 [first, last) 提供。 |
− | * 若 {{tt|path::value_type}} 是 {{c|char}} 且原生编码为 UTF-8 | + | * 若 {{tt|path::value_type}} 是 {{c|char}} 且原生编码为 UTF-8,则如同以 {{c|path(source)}} 或 {{c|path(first, last)}} 构造 。这是使用 Unicode 的 POSIX 系统的典型情况,例如 Linux。 |
− | * 否则,若 {{tt|path::value_type}} 是 {{c|wchar_t}} 且原生编码是 UTF-16 (这是 Windows 上的情况),或若 {{tt|path::value_type}} 是 {{c|char16_t}} | + | * 否则,若 {{tt|path::value_type}} 是 {{c|wchar_t}} 且原生编码是 UTF-16 (这是 Windows 上的情况),或若 {{tt|path::value_type}} 是 {{c|char16_t}}(原生编码保证为 UTF-16)或 {{c|char32_t}}(原生编码保证为 UTF-32),则首先转换 UTF-8 字符序列为 {{tt|path::string_type}} 类型的临时字符串 {{tt|tmp}},然后如同以 {{c|path(tmp)}} 构造新 |
− | UTF-32),则首先转换 UTF-8 字符序列为 {{tt|path::string_type}} 类型的临时字符串 {{tt|tmp}} | + | * 否则(对于非 UTF-8 窄字符编码与非 UTF-16 {{c|wchar_t}}),首先转换 UTF-8 字符序列到 {{c|std::u32string}} 类型的临时 UTF-32 编码字符串 {{tt|tmp}},然后如同用 {{c|path(tmp)}} 构造新 (使用非 Unicode 多字节或单字节编码的文件系统的 POSIX 系统上 ) |
− | * 否则(对于非 UTF-8 窄字符编码与非 UTF-16 {{c|wchar_t}} | + | |
===参数=== | ===参数=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | source | UTF-8 编码的 {{lc|std::string}} | + | {{par | source | UTF-8 编码的 {{lc|std::string}}、{{lc|std::string_view}},指向空终止多字节字符串的指针,或指向空终止多字节字符串的以 char 为 的输入迭代器}} |
− | {{par | first, last | 一对指定 UTF-8 编码字符序列的{{named req|InputIterator | + | {{par | first, last | 一对指定 UTF-8 编码字符序列的{{named req|InputIterator}}}} |
{{par hreq}} | {{par hreq}} | ||
{{par req named | InputIt | InputIterator}} | {{par req named | InputIt | InputIterator}} | ||
− | {{par req | {{tt|Source}} 或 {{tt|InputIt}} 的值类型必须是 {{c|char}}{{rev inl|since=c++20| 或 {{c|char8_t}}}}}} | + | {{par req | {{tt|Source}} 或 {{tt|InputIt}} 的值类型必须是 {{c|char}}{{rev inl|since=c++20| 或 {{c|char8_t}}}} }} |
{{par end}} | {{par end}} | ||
第33行: | 第32行: | ||
===异常=== | ===异常=== | ||
− | 若内存分配失败则可能抛出 {{ | + | 若内存分配失败则可能抛出 {{|std::bad_alloc}}。 |
===注解=== | ===注解=== | ||
− | 在原生路径格式异于通用路径格式的系统上 | + | 在原生路径格式异于通用路径格式的系统上 与 POSIX 均不是此种系统的例子),若此函数的参数使用通用格式,则它会被转换成原生格式。 |
===示例=== | ===示例=== | ||
第43行: | 第42行: | ||
===参阅=== | ===参阅=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/filesystem/dsc path}} | + | {{dsc inc|cpp/filesystem/dsc path}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|en|es|ja|ru}} | {{langlinks|en|es|ja|ru}} |
2023年12月10日 (日) 21:55的最后版本
在标头 <filesystem> 定义
|
||
template< class Source > std::filesystem::path u8path( const Source& source ); |
(1) | (C++17 起) (C++20 弃用) |
template< class InputIt > std::filesystem::path u8path( InputIt first, InputIt last ); |
(2) | (C++17 起) (C++20 弃用) |
从 UTF-8 编码的 char 或 char8_t(C++20 起) 序列构造路径 p
,源作为 std::string 或 std::string_view,或空终止多字节字符串,或作为一对迭代器 [first, last) 提供。
- 若
path::value_type
是 char 且原生编码为 UTF-8,则如同以 path(source) 或 path(first, last) 直接构造路径。这是使用 Unicode 的 POSIX 系统的典型情况,例如 Linux。 - 否则,若
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,指向空终止多字节字符串的指针,或指向空终止多字节字符串的以 char 为值类型的输入迭代器 |
first, last | - | 一对指定 UTF-8 编码字符序列的老式输入迭代器 (LegacyInputIterator) |
类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-Source 或 InputIt 的值类型必须是 char 或 char8_t(C++20 起)。
|
[编辑] 返回值
将输入字符串从 UTF-8 转换到文件系统原生字符编码后,构造的路径。
[编辑] 异常
若内存分配失败则可能抛出 std::bad_alloc。
[编辑] 注解
在原生路径格式异于通用路径格式的系统上(Windows 与 POSIX 均不是此种系统的例子),若此函数的参数使用通用格式,则它会被转换成原生格式。
[编辑] 示例
运行此代码
#include <cstdio> #ifdef _MSC_VER #include <fcntl.h> #include <io.h> #else #include <clocale> #include <locale> #endif #include <filesystem> #include <fstream> int main() { #ifdef _MSC_VER _setmode(_fileno(stderr), _O_WTEXT); #else std::setlocale(LC_ALL, ""); std::locale::global(std::locale("")); #endif std::filesystem::path p(u8"要らない.txt"); std::ofstream(p) << "文件内容"; // 在 LWG2676 前的 MSVC 上使用 operator string_type(), // 其中 string_type 是 wstring,仅根据非标准扩展工作。 // LWG2676 后使用新的 fstream 构造函数 // 原生字符串表示可用于 OS 专有 API #ifdef _MSC_VER if (std::FILE* f = _wfopen(p.c_str(), L"r")) #else if (std::FILE* f = std::fopen(p.c_str(), "r")) #endif { for (int ch; (ch = fgetc(f)) != EOF; std::putchar(ch)) {} std::fclose(f); } std::filesystem::remove(p); }
可能的输出:
文件内容
[编辑] 参阅
(C++17) |
表示路径 (类) |