Convert a string to std filesystem path

僤鯓⒐⒋嵵緔 提交于 2019-12-08 16:43:42

问题


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 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.



来源:https://stackoverflow.com/questions/43114174/convert-a-string-to-std-filesystem-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!