Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string to std filesystem path

Tags:

c++

c++17

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?
like image 342
Roi Danton Avatar asked Mar 30 '17 09:03

Roi Danton


1 Answers

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 an std::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.

like image 54
Barry Avatar answered Sep 16 '22 13:09

Barry