2010-08-22 00:59:46 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2010-11-16 22:09:02 +00:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-08-22 00:59:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// type_traits
|
|
|
|
|
|
|
|
// is_convertible
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
template <class T, class U>
|
|
|
|
void test_is_convertible()
|
|
|
|
{
|
|
|
|
static_assert((std::is_convertible<T, U>::value), "");
|
|
|
|
static_assert((std::is_convertible<const T, U>::value), "");
|
|
|
|
static_assert((std::is_convertible<T, const U>::value), "");
|
|
|
|
static_assert((std::is_convertible<const T, const U>::value), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
void test_is_not_convertible()
|
|
|
|
{
|
|
|
|
static_assert((!std::is_convertible<T, U>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const T, U>::value), "");
|
|
|
|
static_assert((!std::is_convertible<T, const U>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const T, const U>::value), "");
|
|
|
|
}
|
|
|
|
|
2010-08-22 00:59:46 +00:00
|
|
|
typedef void Function();
|
|
|
|
typedef char Array[1];
|
|
|
|
|
Eliminate the C++0x-only is_convertible testing function that accepts
a cv-qualifier rvalue reference to the type, e.g.,
template <class _Tp> char __test(const volatile typename remove_reference<_Tp>::type&&);
The use of this function signature rather than the more
straightforward one used in C++98/03 mode, e.g.,
template <class _Tp> char __test(_Tp);
is broken in two ways:
1) An rvalue reference cannot bind to lvalues, so is_convertible<X&,
X&>::value would be false. This breaks two of the unique_ptr tests
on Clang and GCC >= 4.5. Prior GCC's seem to have allowed rvalue
references to bind to lvalues, allowing this bug to slip in.
2) By adding cv-qualifiers to the type we're converting to, we get
some incorrect "true" results for, e.g., is_convertible<const X&, X&>::value.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@124166 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-25 01:15:41 +00:00
|
|
|
class NonCopyable {
|
|
|
|
NonCopyable(NonCopyable&);
|
|
|
|
};
|
|
|
|
|
2010-08-22 00:59:46 +00:00
|
|
|
int main()
|
|
|
|
{
|
2013-07-04 00:10:01 +00:00
|
|
|
// void
|
|
|
|
test_is_convertible<void,void> ();
|
|
|
|
test_is_not_convertible<void,Function> ();
|
|
|
|
test_is_not_convertible<void,Function&> ();
|
|
|
|
test_is_not_convertible<void,Function*> ();
|
|
|
|
test_is_not_convertible<void,Array> ();
|
|
|
|
test_is_not_convertible<void,Array&> ();
|
|
|
|
test_is_not_convertible<void,char> ();
|
|
|
|
test_is_not_convertible<void,char&> ();
|
|
|
|
test_is_not_convertible<void,char*> ();
|
|
|
|
|
|
|
|
// Function
|
|
|
|
test_is_not_convertible<Function, void> ();
|
|
|
|
test_is_not_convertible<Function, Function> ();
|
|
|
|
test_is_convertible<Function, Function&> ();
|
|
|
|
test_is_convertible<Function, Function*> ();
|
|
|
|
test_is_not_convertible<Function, Array> ();
|
|
|
|
test_is_not_convertible<Function, Array&> ();
|
|
|
|
test_is_not_convertible<Function, char> ();
|
|
|
|
test_is_not_convertible<Function, char&> ();
|
|
|
|
test_is_not_convertible<Function, char*> ();
|
|
|
|
|
|
|
|
// Function&
|
|
|
|
test_is_not_convertible<Function&, void> ();
|
|
|
|
test_is_not_convertible<Function&, Function> ();
|
|
|
|
test_is_convertible<Function&, Function&> ();
|
|
|
|
|
|
|
|
test_is_convertible<Function&, Function*> ();
|
|
|
|
test_is_not_convertible<Function&, Array> ();
|
|
|
|
test_is_not_convertible<Function&, Array&> ();
|
|
|
|
test_is_not_convertible<Function&, char> ();
|
|
|
|
test_is_not_convertible<Function&, char&> ();
|
|
|
|
test_is_not_convertible<Function&, char*> ();
|
|
|
|
|
|
|
|
// Function*
|
|
|
|
test_is_not_convertible<Function*, void> ();
|
|
|
|
test_is_not_convertible<Function*, Function> ();
|
|
|
|
test_is_not_convertible<Function*, Function&> ();
|
|
|
|
test_is_convertible<Function*, Function*> ();
|
|
|
|
|
|
|
|
test_is_not_convertible<Function*, Array> ();
|
|
|
|
test_is_not_convertible<Function*, Array&> ();
|
|
|
|
test_is_not_convertible<Function*, char> ();
|
|
|
|
test_is_not_convertible<Function*, char&> ();
|
|
|
|
test_is_not_convertible<Function*, char*> ();
|
|
|
|
|
|
|
|
// Array
|
|
|
|
test_is_not_convertible<Array, void> ();
|
|
|
|
test_is_not_convertible<Array, Function> ();
|
|
|
|
test_is_not_convertible<Array, Function&> ();
|
|
|
|
test_is_not_convertible<Array, Function*> ();
|
|
|
|
test_is_not_convertible<Array, Array> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
|
|
|
static_assert((!std::is_convertible<Array, Array&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<Array, const Array&>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const Array, Array&>::value), "");
|
2011-01-28 20:00:37 +00:00
|
|
|
static_assert(( std::is_convertible<const Array, const Array&>::value), "");
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
test_is_not_convertible<Array, char> ();
|
|
|
|
test_is_not_convertible<Array, char&> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
|
|
|
static_assert(( std::is_convertible<Array, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<Array, const char*>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const Array, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const Array, const char*>::value), "");
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
// Array&
|
|
|
|
test_is_not_convertible<Array&, void> ();
|
|
|
|
test_is_not_convertible<Array&, Function> ();
|
|
|
|
test_is_not_convertible<Array&, Function&> ();
|
|
|
|
test_is_not_convertible<Array&, Function*> ();
|
|
|
|
test_is_not_convertible<Array&, Array> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
|
|
|
static_assert(( std::is_convertible<Array&, Array&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<Array&, const Array&>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const Array&, Array&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const Array&, const Array&>::value), "");
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
test_is_not_convertible<Array&, char> ();
|
|
|
|
test_is_not_convertible<Array&, char&> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
|
|
|
static_assert(( std::is_convertible<Array&, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<Array&, const char*>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const Array&, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const Array&, const char*>::value), "");
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
// char
|
|
|
|
test_is_not_convertible<char, void> ();
|
|
|
|
test_is_not_convertible<char, Function> ();
|
|
|
|
test_is_not_convertible<char, Function&> ();
|
|
|
|
test_is_not_convertible<char, Function*> ();
|
|
|
|
test_is_not_convertible<char, Array> ();
|
|
|
|
test_is_not_convertible<char, Array&> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-08 21:06:38 +00:00
|
|
|
test_is_convertible<char, char> ();
|
|
|
|
|
2010-08-22 00:59:46 +00:00
|
|
|
static_assert((!std::is_convertible<char, char&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<char, const char&>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const char, char&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const char, const char&>::value), "");
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
test_is_not_convertible<char, char*> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
// char&
|
|
|
|
test_is_not_convertible<char&, void> ();
|
|
|
|
test_is_not_convertible<char&, Function> ();
|
|
|
|
test_is_not_convertible<char&, Function&> ();
|
|
|
|
test_is_not_convertible<char&, Function*> ();
|
|
|
|
test_is_not_convertible<char&, Array> ();
|
|
|
|
test_is_not_convertible<char&, Array&> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-08 21:06:38 +00:00
|
|
|
test_is_convertible<char&, char> ();
|
|
|
|
|
2010-08-22 00:59:46 +00:00
|
|
|
static_assert(( std::is_convertible<char&, char&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<char&, const char&>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const char&, char&>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const char&, const char&>::value), "");
|
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
test_is_not_convertible<char&, char*> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-04 00:10:01 +00:00
|
|
|
// char*
|
|
|
|
test_is_not_convertible<char*, void> ();
|
|
|
|
test_is_not_convertible<char*, Function> ();
|
|
|
|
test_is_not_convertible<char*, Function&> ();
|
|
|
|
test_is_not_convertible<char*, Function*> ();
|
|
|
|
test_is_not_convertible<char*, Array> ();
|
|
|
|
test_is_not_convertible<char*, Array&> ();
|
2010-08-22 00:59:46 +00:00
|
|
|
|
2013-07-08 21:06:38 +00:00
|
|
|
test_is_not_convertible<char*, char> ();
|
|
|
|
test_is_not_convertible<char*, char&> ();
|
|
|
|
|
2010-08-22 00:59:46 +00:00
|
|
|
static_assert(( std::is_convertible<char*, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<char*, const char*>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const char*, char*>::value), "");
|
|
|
|
static_assert(( std::is_convertible<const char*, const char*>::value), "");
|
2013-07-04 00:10:01 +00:00
|
|
|
|
|
|
|
// NonCopyable
|
Eliminate the C++0x-only is_convertible testing function that accepts
a cv-qualifier rvalue reference to the type, e.g.,
template <class _Tp> char __test(const volatile typename remove_reference<_Tp>::type&&);
The use of this function signature rather than the more
straightforward one used in C++98/03 mode, e.g.,
template <class _Tp> char __test(_Tp);
is broken in two ways:
1) An rvalue reference cannot bind to lvalues, so is_convertible<X&,
X&>::value would be false. This breaks two of the unique_ptr tests
on Clang and GCC >= 4.5. Prior GCC's seem to have allowed rvalue
references to bind to lvalues, allowing this bug to slip in.
2) By adding cv-qualifiers to the type we're converting to, we get
some incorrect "true" results for, e.g., is_convertible<const X&, X&>::value.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@124166 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-25 01:15:41 +00:00
|
|
|
static_assert((std::is_convertible<NonCopyable&, NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<NonCopyable&, const NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<NonCopyable&, const volatile NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<NonCopyable&, volatile NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<const NonCopyable&, const NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<const NonCopyable&, const volatile NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<volatile NonCopyable&, const volatile NonCopyable&>::value), "");
|
|
|
|
static_assert((std::is_convertible<const volatile NonCopyable&, const volatile NonCopyable&>::value), "");
|
|
|
|
static_assert((!std::is_convertible<const NonCopyable&, NonCopyable&>::value), "");
|
2010-08-22 00:59:46 +00:00
|
|
|
}
|