Nikki Chumakov
Nikki Chumakov

Reputation: 1287

Sequencing rules and side effects for the operators

Is the following code have well defined behavior:

#include <boost/filesystem.hpp>
...
void foo (fs::path const& dir, fs::path file)
{
  file = dir / std::move (file); // Is it ok?
}

if we assume that we have operator/ (path const& a, path&& b) that modifies its second rvalue argument?

PS. This is not a boost-specific question. Boost filesystem library was only used as a example of the context where such a question may arise. The question is about of the safety of using x = y / move(x) expressions in C++, when x,y is a classes and operator/ takes rvalue reference and can modify it.

Upvotes: 2

Views: 102

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 72054

Your code is equivalent to this one, where the overloaded operators are resolved:

file.operator=(operator/(dir, std::move(file));

This is safe. Evaluation of the function arguments is sequenced before the function call, and although the evaluation of the method subject is unsequenced with respect to arguments, there is nothing to do there, so the modification of file cannot interfere with simply using file as an lvalue.

Upvotes: 3

Related Questions