aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.fail.cpp
blob: 07578a28e82f0e9b97106719c75943b008113ff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//===----------------------------------------------------------------------===//
//
//                     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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14

// <any>

// template <class ValueType>
// ValueType any_cast(any &&);

// Try and use the rvalue any_cast to cast to an lvalue reference

#include <any>

struct TestType {};
using std::any;
using std::any_cast;

void test_const_lvalue_cast_request_non_const_lvalue()
{
    const any a;
    // expected-error@any:* {{static_assert failed "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
    // expected-error@any:* {{binding value of type 'const TestType' to reference to type 'TestType' drops 'const' qualifier}}
    any_cast<TestType &>(a); // expected-note {{requested here}}

    const any a2(42);
    // expected-error@any:* {{static_assert failed "ValueType is required to be a const lvalue reference or a CopyConstructible type"}}
    // expected-error@any:* {{binding value of type 'const int' to reference to type 'int' drops 'const' qualifier}}
    any_cast<int&>(a2); // expected-note {{requested here}}
}

void test_lvalue_any_cast_request_rvalue()
{
    any a;
    // expected-error@any:* {{static_assert failed "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
    any_cast<TestType &&>(a); // expected-note {{requested here}}

    any a2(42);
    // expected-error@any:* {{static_assert failed "ValueType is required to be an lvalue reference or a CopyConstructible type"}}
    any_cast<int&&>(a2); // expected-note {{requested here}}
}

void test_rvalue_any_cast_request_lvalue()
{
    any a;
    // expected-error@any:* {{static_assert failed "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
    // expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}}
    any_cast<TestType &>(std::move(a)); // expected-note {{requested here}}

    // expected-error@any:* {{static_assert failed "ValueType is required to be an rvalue reference or a CopyConstructible type"}}
    // expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}}
    any_cast<int&>(42);
}

int main()
{
    test_const_lvalue_cast_request_non_const_lvalue();
    test_lvalue_any_cast_request_rvalue();
    test_rvalue_any_cast_request_lvalue();
}