c++ - Does swap() cause undefined behaviour? -



c++ - Does swap() cause undefined behaviour? -

i'm trying understand conditions on std::swap [c++11: utility.swap]. template defined as

template <typename t> void swap(t &, t &)

(plus noexcept details) , having effect of "exchanging values stored @ 2 locations".

is next programme have well-defined?

#include <utility> int main() { int m, n; std::swap(m, n); }

if wrote swap code myself (i.e. int tmp = m; m = n; n = tmp;), have undefined behaviour, since effort lvalue-to-rvalue conversion on uninitialized object. standard std::swap function not seem come conditions imposed on it, nor can 1 derive specification there lvalue-to-rvalue , ub.

does standard require std::swap perform magic well-defined on uninitialized objects?

to clarify point, consider function void f(int & n) { n = 25; }, never has undefined behaviour (since not read n).

very nice question. however, covered [res.on.arguments]§1:

each of next applies arguments functions defined in c++ standard library, unless explicitly stated otherwise.

if argument function has invalid value (such value outside domain of function or pointer invalid intended use), behavior undefined.

to address concern f(n), function f question not part of c++ standard library , above clause not apply it.

c++ c++11 language-lawyer undefined-behavior lvalue-to-rvalue

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -