close
Namespaces
Variants

Talk:cpp/filesystem/path/replace filename

From cppreference.com

The second part of the example seems to be UB as it has no filename? Should this be changed?

             << fs::path("/").replace_filename("bar") << '\n';


Best, Andreas 91.103.43.254 07:54, 14 September 2020 (PDT)

The example is ok. Here is an excerpt from the standard [n4860]:
path& replace_filename(const path& replacement);
Effects: Equivalent to:
    remove_filename();
    operator/=(replacement);
Returns: *this.
[Example:
    path("/foo").replace_filename("bar");   // yields "/bar" on POSIX
    path("/").replace_filename("bar");      // yields "/bar" on POSIX
 end example]
UB is not mentioned at all. It seems that the statement
"The behavior is undefined if the path has no filename component"
should be removed from the page [replace_filename].
Ok, then the sentence should be removed and also the output on the page of the second example should be corrected from "bar" to "/bar". Correct?
Oh, yes. Thank you. Let's fix all that.