]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/result_of/sfinae_friendly_2.cc
Update copyright in libstdc++-v3.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / result_of / sfinae_friendly_2.cc
1 // { dg-options "-std=gnu++11" }
2 // { dg-do compile }
3
4 // Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // Taken from N3436:
22
23 #include <type_traits>
24 #include <string>
25
26 struct eat { template<typename T> eat(T const &) {} };
27 struct not_incrementable {};
28
29 struct inc {
30 template<typename T>
31 auto operator()(T t) const -> decltype(t++)
32 { return t++; }
33 };
34
35 template<typename A>
36 typename std::result_of<inc(A)>::type // sfinae here
37 try_inc(A a) {
38 return inc()(a);
39 }
40
41 not_incrementable
42 try_inc(eat) {
43 return not_incrementable();
44 }
45
46 template<typename>
47 struct never { static const bool value = false; };
48
49 template<typename T>
50 struct Fail
51 {
52 static_assert(never<T>::value, "duh");
53 typedef int type;
54 };
55
56 struct Fun
57 {
58 template<typename T>
59 typename Fail<T>::type operator()(T)
60 { return 0; }
61 };
62
63 template<typename T>
64 typename std::result_of<Fun(T)>::type foo(T)
65 { return 0; }
66
67 template<typename>
68 int foo(...)
69 { return 0; }
70
71 void result_of_sfinae() {
72 int x = try_inc(1); // OK
73 not_incrementable y = try_inc(std::string("foo")); // OK, not_incrementable
74 (void) x;
75 (void) y;
76 }