]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/type_traits/detection.cc
Use effective-target instead of -std options
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / type_traits / detection.cc
CommitLineData
818ab71a 1// Copyright (C) 2015-2016 Free Software Foundation, Inc.
6af6bef4
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
52066eae 18// { dg-do compile { target c++14 } }
6af6bef4
JW
19
20#include <experimental/type_traits>
21
22using std::declval;
23using std::ptrdiff_t;
24using std::experimental::is_detected;
25using std::experimental::is_detected_exact;
26using std::experimental::detected_or_t;
27using std::experimental::is_same_v;
28
29// Examples taken from N4502
30
31// archetypal helper alias for a copy assignment operation:
32template <class T>
33using copy_assign_t = decltype(declval<T&>() = declval<T const &>());
34
35// plausible implementation for the is_assignable type trait:
36template <class T>
37 using is_copy_assignable = is_detected<copy_assign_t, T>;
38
39// plausible implementation for an augmented is_assignable type trait
40// that also checks the return type:
41template <class T>
42 using is_canonical_copy_assignable = is_detected_exact<T&, copy_assign_t, T>;
43
44struct A { };
45struct B { B& operator=(const B&); };
46struct C { void operator=(const C&); };
47struct D { D& operator=(D&); };
48struct E { E& operator=(E&&); };
49
50static_assert( is_copy_assignable<A>::value, "A is copy assignable" );
51static_assert( is_copy_assignable<B>::value, "B is copy assignable" );
52static_assert( is_copy_assignable<C>::value, "C is copy assignable" );
53static_assert( !is_copy_assignable<D>::value, "D is not copy assignable" );
54static_assert( !is_copy_assignable<E>::value, "E is not copy assignable" );
55
56static_assert( is_canonical_copy_assignable<A>::value,
57 "A has canonical copy assignment" );
58static_assert( is_canonical_copy_assignable<B>::value,
59 "B has canonical copy assignment" );
60static_assert( !is_canonical_copy_assignable<C>::value,
61 "C does not have canonical copy assignment" );
62static_assert( !is_canonical_copy_assignable<D>::value,
63 "D does not have canonical copy assignment" );
64static_assert( !is_canonical_copy_assignable<E>::value,
65 "E does not have canonical copy assignment" );
66
67// archetypal helper alias for a particular type member:
68template <class T>
69 using diff_t = typename T::difference_type;
70// alias the type member, if it exists, otherwise alias ptrdiff_t:
71template <class Ptr>
72 using difference_type = detected_or_t<ptrdiff_t, diff_t, Ptr>;
73
74struct has { using difference_type = char; };
75struct has_not { };
76struct inherits : has { };
77struct hides : private has { };
78struct reveals : private has { using has::difference_type; };
79
80static_assert( is_same_v<difference_type<has>, char>, "has" );
81static_assert( is_same_v<difference_type<has_not>, ptrdiff_t>, "has not" );
82static_assert( is_same_v<difference_type<inherits>, char>, "inherits" );
83static_assert( is_same_v<difference_type<hides>, ptrdiff_t>, "hides" );
84static_assert( is_same_v<difference_type<reveals>, char>, "reveals" );