问题
A file path is passed as a string. How do I convert this string to a std::filesystem::path? Example:
#include <filesystem>
std::string inputPath = "a/custom/path.ext";
const std::filesystem::path path = inputPath; // Is this assignment safe?
回答1:
Yes, this construction is safe:
const std::filesystem::path path = inputPath; // Is this assignment safe?
That is not assignment, that is copy initialization. You are invoking this constructor:
template< class Source >
path( const Source& source );
which takes:
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
,
So you're fine. Plus, it would be really weird if you couldn't construct a filesystem::path
from a std::string
.
来源:https://stackoverflow.com/questions/43114174/convert-a-string-to-std-filesystem-path