std::filesystem::path::append, std::filesystem::path::operator/=

  1. If p.is_absolute() || (p.has_root_name() && p.root_name() != root_name()), then replaces the current path with p as if by operator=(p) and finishes.

# Declarations

path& operator/=( const path& p );

(since C++17)

template< class Source >
path& operator/=( const Source& source );

(since C++17)

template< class Source >
path& append( const Source& source );

(since C++17)

template< class InputIt >
path& append( InputIt first, InputIt last );

(since C++17)

# Parameters

# Return value

*this

# Notes

These functions effectively yield an approximation of the meaning of the argument path p in an environment where *this is the starting directory.

# Example

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    fs::path p1 = "C:";
    p1 /= "Users"; // does not insert a separator
    std::cout << "\"C:\" / \"Users\" == " << p1 << '\n';
    p1 /= "batman"; // inserts fs::path::preferred_separator, '\' on Windows
    std::cout << "\"C:\" / \"Users\" / \"batman\" == " << p1 << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3244C++17constraint that Source cannot be path was missingadded

# See also