const_correctness.fail.cpp (2370B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03, c++11, c++14 11 12 // <any> 13 14 // template <class ValueType> 15 // ValueType any_cast(any const &); 16 17 // Try and cast away const. 18 19 #include <any> 20 21 struct TestType {}; 22 struct TestType2 {}; 23 24 // On platforms that do not support any_cast, an additional availability error 25 // is triggered by these tests. 26 // expected-error@const_correctness.fail.cpp:* 0+ {{call to unavailable function 'any_cast': introduced in macOS 10.14}} 27 28 int main() 29 { 30 using std::any; 31 using std::any_cast; 32 33 any a; 34 35 // expected-error@any:* {{binding value of type 'const TestType' to reference to type 'TestType' drops 'const' qualifier}} 36 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}} 37 any_cast<TestType &>(static_cast<any const&>(a)); // expected-note {{requested here}} 38 39 // expected-error@any:* {{cannot cast from lvalue of type 'const TestType' to rvalue reference type 'TestType &&'; types are not compatible}} 40 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}} 41 any_cast<TestType &&>(static_cast<any const&>(a)); // expected-note {{requested here}} 42 43 // expected-error@any:* {{binding value of type 'const TestType2' to reference to type 'TestType2' drops 'const' qualifier}} 44 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}} 45 any_cast<TestType2 &>(static_cast<any const&&>(a)); // expected-note {{requested here}} 46 47 // expected-error@any:* {{cannot cast from lvalue of type 'const TestType2' to rvalue reference type 'TestType2 &&'; types are not compatible}} 48 // expected-error-re@any:* {{static_assert failed{{.*}} "ValueType is required to be a const lvalue reference or a CopyConstructible type"}} 49 any_cast<TestType2 &&>(static_cast<any const&&>(a)); // expected-note {{requested here}} 50 }