Files
libcxx/test/std/utilities/utility/forward/forward.pass.cpp
Chandler Carruth 7c3769df62 Update more file headers across all of the LLVM projects in the monorepo
to reflect the new license. These used slightly different spellings that
defeated my regular expressions.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@351648 91177308-0d34-0410-b5e6-96231b3b80d8
2019-01-19 10:56:40 +00:00

91 lines
3.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03
// test forward
#include <utility>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
struct A
{
};
A source() noexcept {return A();}
const A csource() noexcept {return A();}
constexpr bool test_constexpr_forward() {
#if TEST_STD_VER > 11
int x = 42;
const int cx = 101;
return std::forward<int&>(x) == 42
&& std::forward<int>(x) == 42
&& std::forward<const int&>(x) == 42
&& std::forward<const int>(x) == 42
&& std::forward<int&&>(x) == 42
&& std::forward<const int&&>(x) == 42
&& std::forward<const int&>(cx) == 101
&& std::forward<const int>(cx) == 101;
#else
return true;
#endif
}
int main()
{
A a;
const A ca = A();
((void)a); // Prevent unused warning
((void)ca); // Prevent unused warning
static_assert(std::is_same<decltype(std::forward<A&>(a)), A&>::value, "");
static_assert(std::is_same<decltype(std::forward<A>(a)), A&&>::value, "");
static_assert(std::is_same<decltype(std::forward<A>(source())), A&&>::value, "");
static_assert(noexcept(std::forward<A&>(a)), "");
static_assert(noexcept(std::forward<A>(a)), "");
static_assert(noexcept(std::forward<A>(source())), "");
static_assert(std::is_same<decltype(std::forward<const A&>(a)), const A&>::value, "");
static_assert(std::is_same<decltype(std::forward<const A>(a)), const A&&>::value, "");
static_assert(std::is_same<decltype(std::forward<const A>(source())), const A&&>::value, "");
static_assert(noexcept(std::forward<const A&>(a)), "");
static_assert(noexcept(std::forward<const A>(a)), "");
static_assert(noexcept(std::forward<const A>(source())), "");
static_assert(std::is_same<decltype(std::forward<const A&>(ca)), const A&>::value, "");
static_assert(std::is_same<decltype(std::forward<const A>(ca)), const A&&>::value, "");
static_assert(std::is_same<decltype(std::forward<const A>(csource())), const A&&>::value, "");
static_assert(noexcept(std::forward<const A&>(ca)), "");
static_assert(noexcept(std::forward<const A>(ca)), "");
static_assert(noexcept(std::forward<const A>(csource())), "");
#if TEST_STD_VER > 11
{
constexpr int i2 = std::forward<int>(42);
static_assert(std::forward<int>(42) == 42, "");
static_assert(std::forward<const int&>(i2) == 42, "");
static_assert(test_constexpr_forward(), "");
}
#endif
#if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
// Test that std::forward is constexpr in C++11. This is an extension
// provided by both libc++ and libstdc++.
{
constexpr int i2 = std::forward<int>(42);
static_assert(std::forward<int>(42) == 42, "" );
static_assert(std::forward<const int&>(i2) == 42, "");
}
#endif
}