Implement n3584 - Addressing Tuples by Type

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@186237 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2013-07-13 02:54:05 +00:00
parent ef7a7b730c
commit e8029e54f1
11 changed files with 387 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <tuple>
#include <string>
#include <complex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::complex<float> cf;
{
auto t1 = std::tuple<int, std::string, cf> { 42, "Hi", { 1,2 }};
assert ( std::get<int>(t1) == 42 ); // find at the beginning
assert ( std::get<std::string>(t1) == "Hi" ); // find in the middle
assert ( std::get<cf>(t1).real() == 1 ); // find at the end
assert ( std::get<cf>(t1).imag() == 2 );
}
{
auto t2 = std::tuple<int, std::string, int, cf> { 42, "Hi", 23, { 1,2 }};
// get<int> would fail!
assert ( std::get<std::string>(t2) == "Hi" );
assert (( std::get<cf>(t2) == cf{ 1,2 } ));
}
{
const std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 };
const int &i1 = std::get<int>(p5);
const int &i2 = std::get<const int>(p5);
assert ( i1 == 1 );
assert ( i2 == 2 );
}
{
typedef std::unique_ptr<int> upint;
std::tuple<upint> t(upint(new int(4)));
upint p = std::get<upint>(std::move(t)); // get rvalue
assert(*p == 4);
assert(std::get<0>(t) == nullptr); // has been moved from
}
#endif
}

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <tuple>
#include <string>
#include <complex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::complex<float> cf;
auto t1 = std::make_tuple<int, std::string> ( 42, "Hi" );
assert ( std::get<cf>(t1) == cf {1,2} ); // no such type
#else
#error
#endif
}

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <tuple>
#include <string>
#include <complex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::complex<float> cf;
auto t1 = std::make_tuple<int, int, std::string, cf> ( 42, 21, "Hi", { 1,2 } );
assert ( std::get<int>(t1) == 42 ); // two ints here
#else
#error
#endif
}

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <tuple>
#include <string>
#include <complex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::complex<float> cf;
auto t1 = std::make_tuple<double, int, std::string, cf, int> ( 42, 21, "Hi", { 1,2 } );
assert ( std::get<int>(t1) == 42 ); // two ints here (one at the end)
#else
#error
#endif
}

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <tuple>
#include <string>
#include <memory>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::unique_ptr<int> upint;
std::tuple<upint> t(upint(new int(4)));
upint p = std::get<upint>(t);
#else
#error
#endif
}