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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With