Namespaces
Variants

std::filesystem::path:: make_preferred

From cppreference.net
path & make_preferred ( ) ;
(C++17부터)

경로의 일반 형식 뷰에 있는 모든 디렉토리 구분자를 선호하는 디렉토리 구분자로 변환합니다.

예를 들어, Windows에서는 \ 가 선호되는 구분자이며, 경로 foo / bar foo\bar 로 변환됩니다.

목차

매개변수

(없음)

반환값

* this

예외

구현 정의 예외를 던질 수 있습니다.

예제

Windows는 / 를 구분자로 사용할 수 있지만 \ 를 선호하므로 make_preferred 가 슬래시를 백슬래시로 변환합니다. 반면 POSIX는 백슬래시를 유효한 파일명 문자로 사용하기 때문에 \ 를 구분자로 사용하지 않습니다 — POSIX에서의 Windows 경로는 실제로 "a \\ b \\ c" 라는 이름의 파일을 참조합니다. 이러한 이유로 "구분자"는 변환되지 않습니다.

#include <filesystem>
#include <iostream>
int main()
{
    std::filesystem::path
        windows_path("a\\b\\c"),
        posix_path("a/b/c");
    std::cout
        << "Windows path: "
        << windows_path << " -> "
        << windows_path.make_preferred() << '\n'
        << "POSIX path: "
        << posix_path << " -> "
        << posix_path.make_preferred() << '\n';
}

출력:

// on Windows
Windows path: "a\\b\\c" -> "a\\b\\c"
POSIX path: "a/b/c" -> "a\\b\\c"
// on POSIX
Windows path: "a\\b\\c" -> "a\\b\\c"
POSIX path: "a/b/c" -> "a/b/c"

참고 항목

constexpr value_type preferred_separator
[static]
이식 가능한 / 에 추가적으로 사용될 수 있는 대체 디렉토리 구분자. Windows에서는 백슬래시 문자 \ 입니다. POSIX에서는 이식 가능한 구분자와 동일한 슬래시 / 입니다.
(public static member constant)