summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/type_traits/detection.cc
blob: 45d06921f05cc11f27bf5187cc584824a778098a (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++14" }
// { dg-do compile }

#include <experimental/type_traits>

using std::declval;
using std::ptrdiff_t;
using std::experimental::is_detected;
using std::experimental::is_detected_exact;
using std::experimental::detected_or_t;
using std::experimental::is_same_v;

// Examples taken from N4502

// archetypal helper alias for a copy assignment operation:
template <class T>
using copy_assign_t = decltype(declval<T&>() = declval<T const &>());

// plausible implementation for the is_assignable type trait:
template <class T>
  using is_copy_assignable = is_detected<copy_assign_t, T>;

// plausible implementation for an augmented is_assignable type trait
// that also checks the return type:
template <class T>
  using is_canonical_copy_assignable = is_detected_exact<T&, copy_assign_t, T>;

struct A { };
struct B { B& operator=(const B&); };
struct C { void operator=(const C&); };
struct D { D& operator=(D&); };
struct E { E& operator=(E&&); };

static_assert( is_copy_assignable<A>::value,  "A is copy assignable" );
static_assert( is_copy_assignable<B>::value,  "B is copy assignable" );
static_assert( is_copy_assignable<C>::value,  "C is copy assignable" );
static_assert( !is_copy_assignable<D>::value, "D is not copy assignable" );
static_assert( !is_copy_assignable<E>::value,  "E is not copy assignable" );

static_assert( is_canonical_copy_assignable<A>::value,
               "A has canonical copy assignment" );
static_assert( is_canonical_copy_assignable<B>::value,
               "B has canonical copy assignment" );
static_assert( !is_canonical_copy_assignable<C>::value,
               "C does not have canonical copy assignment" );
static_assert( !is_canonical_copy_assignable<D>::value,
               "D does not have canonical copy assignment" );
static_assert( !is_canonical_copy_assignable<E>::value,
               "E does not have canonical copy assignment" );

// archetypal helper alias for a particular type member:
template <class T>
  using diff_t = typename T::difference_type;
// alias the type member, if it exists, otherwise alias ptrdiff_t:
template <class Ptr>
  using difference_type = detected_or_t<ptrdiff_t, diff_t, Ptr>;

struct has { using difference_type = char; };
struct has_not { };
struct inherits : has { };
struct hides : private has { };
struct reveals : private has { using has::difference_type; };

static_assert( is_same_v<difference_type<has>,      char>,      "has" );
static_assert( is_same_v<difference_type<has_not>,  ptrdiff_t>, "has not" );
static_assert( is_same_v<difference_type<inherits>, char>,      "inherits" );
static_assert( is_same_v<difference_type<hides>,    ptrdiff_t>, "hides" );
static_assert( is_same_v<difference_type<reveals>,  char>,      "reveals" );