1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===// 2*4684ddb6SLionel Sambuc // 3*4684ddb6SLionel Sambuc // The LLVM Compiler Infrastructure 4*4684ddb6SLionel Sambuc // 5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open 6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details. 7*4684ddb6SLionel Sambuc // 8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===// 9*4684ddb6SLionel Sambuc 10*4684ddb6SLionel Sambuc // utilities 11*4684ddb6SLionel Sambuc 12*4684ddb6SLionel Sambuc // exchange 13*4684ddb6SLionel Sambuc 14*4684ddb6SLionel Sambuc #include <utility> 15*4684ddb6SLionel Sambuc #include <cassert> 16*4684ddb6SLionel Sambuc #include <string> 17*4684ddb6SLionel Sambuc main()18*4684ddb6SLionel Sambucint main() 19*4684ddb6SLionel Sambuc { 20*4684ddb6SLionel Sambuc #if _LIBCPP_STD_VER > 11 21*4684ddb6SLionel Sambuc { 22*4684ddb6SLionel Sambuc int v = 12; 23*4684ddb6SLionel Sambuc assert ( std::exchange ( v, 23 ) == 12 ); 24*4684ddb6SLionel Sambuc assert ( v == 23 ); 25*4684ddb6SLionel Sambuc assert ( std::exchange ( v, 67.2 ) == 23 ); 26*4684ddb6SLionel Sambuc assert ( v == 67 ); 27*4684ddb6SLionel Sambuc 28*4684ddb6SLionel Sambuc assert ((std::exchange<int, float> ( v, {} )) == 67 ); 29*4684ddb6SLionel Sambuc assert ( v == 0 ); 30*4684ddb6SLionel Sambuc 31*4684ddb6SLionel Sambuc } 32*4684ddb6SLionel Sambuc 33*4684ddb6SLionel Sambuc { 34*4684ddb6SLionel Sambuc bool b = false; 35*4684ddb6SLionel Sambuc assert ( !std::exchange ( b, true )); 36*4684ddb6SLionel Sambuc assert ( b ); 37*4684ddb6SLionel Sambuc } 38*4684ddb6SLionel Sambuc 39*4684ddb6SLionel Sambuc { 40*4684ddb6SLionel Sambuc const std::string s1 ( "Hi Mom!" ); 41*4684ddb6SLionel Sambuc const std::string s2 ( "Yo Dad!" ); 42*4684ddb6SLionel Sambuc std::string s3 = s1; // Mom 43*4684ddb6SLionel Sambuc assert ( std::exchange ( s3, s2 ) == s1 ); 44*4684ddb6SLionel Sambuc assert ( s3 == s2 ); 45*4684ddb6SLionel Sambuc assert ( std::exchange ( s3, "Hi Mom!" ) == s2 ); 46*4684ddb6SLionel Sambuc assert ( s3 == s1 ); 47*4684ddb6SLionel Sambuc 48*4684ddb6SLionel Sambuc s3 = s2; // Dad 49*4684ddb6SLionel Sambuc assert ( std::exchange ( s3, {} ) == s2 ); 50*4684ddb6SLionel Sambuc assert ( s3.size () == 0 ); 51*4684ddb6SLionel Sambuc 52*4684ddb6SLionel Sambuc s3 = s2; // Dad 53*4684ddb6SLionel Sambuc assert ( std::exchange ( s3, "" ) == s2 ); 54*4684ddb6SLionel Sambuc assert ( s3.size () == 0 ); 55*4684ddb6SLionel Sambuc } 56*4684ddb6SLionel Sambuc 57*4684ddb6SLionel Sambuc #endif 58*4684ddb6SLionel Sambuc } 59