将字符串转换为标准文件系统路径
Convert a string to std filesystem path
文件路径作为字符串传递。如何将此字符串转换为 std::filesystem::path?示例:
#include <filesystem>
std::string inputPath = "a/custom/path.ext";
const std::filesystem::path path = inputPath; // Is this assignment safe?
是的,这个构造是安全的:
const std::filesystem::path path = inputPath; // Is this assignment safe?
那不是赋值,那是复制初始化。您正在调用此 constructor:
template< class Source >
path( const Source& source );
需要:
Constructs the path from a character sequence provided by source (4), which is a pointer or an input iterator to a null-terminated character/wide character sequence, an std::basic_string
or an std::basic_string_view
,
所以你很好。另外,如果您不能从 std::string
构造 filesystem::path
,那将是 真的 奇怪。
文件路径作为字符串传递。如何将此字符串转换为 std::filesystem::path?示例:
#include <filesystem>
std::string inputPath = "a/custom/path.ext";
const std::filesystem::path path = inputPath; // Is this assignment safe?
是的,这个构造是安全的:
const std::filesystem::path path = inputPath; // Is this assignment safe?
那不是赋值,那是复制初始化。您正在调用此 constructor:
template< class Source >
path( const Source& source );
需要:
Constructs the path from a character sequence provided by source (4), which is a pointer or an input iterator to a null-terminated character/wide character sequence, an
std::basic_string
or anstd::basic_string_view
,
所以你很好。另外,如果您不能从 std::string
构造 filesystem::path
,那将是 真的 奇怪。