|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s |
| 2 | +// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s |
| 3 | +// expected-no-diagnostics |
| 4 | + |
| 5 | +namespace std { |
| 6 | + |
| 7 | +template<typename Signature> class function; |
| 8 | + |
| 9 | +template<typename R, typename... Args> class invoker_base { |
| 10 | +public: |
| 11 | + virtual ~invoker_base() { } |
| 12 | + virtual R invoke(Args...) = 0; |
| 13 | + virtual invoker_base* clone() = 0; |
| 14 | +}; |
| 15 | + |
| 16 | +template<typename F, typename R, typename... Args> |
| 17 | +class functor_invoker : public invoker_base<R, Args...> { |
| 18 | +public: |
| 19 | + explicit functor_invoker(const F& f) : f(f) { } |
| 20 | + R invoke(Args... args) { return f(args...); } |
| 21 | + functor_invoker* clone() { return new functor_invoker(f); } |
| 22 | + |
| 23 | +private: |
| 24 | + F f; |
| 25 | +}; |
| 26 | + |
| 27 | +template<typename R, typename... Args> |
| 28 | +class function<R (Args...)> { |
| 29 | +public: |
| 30 | + typedef R result_type; |
| 31 | + function() : invoker (0) { } |
| 32 | + function(const function& other) : invoker(0) { |
| 33 | + if (other.invoker) |
| 34 | + invoker = other.invoker->clone(); |
| 35 | + } |
| 36 | + |
| 37 | + template<typename F> function(const F& f) : invoker(0) { |
| 38 | + invoker = new functor_invoker<F, R, Args...>(f); |
| 39 | + } |
| 40 | + |
| 41 | + ~function() { |
| 42 | + if (invoker) |
| 43 | + delete invoker; |
| 44 | + } |
| 45 | + |
| 46 | + function& operator=(const function& other) { |
| 47 | + function(other).swap(*this); |
| 48 | + return *this; |
| 49 | + } |
| 50 | + |
| 51 | + template<typename F> |
| 52 | + function& operator=(const F& f) { |
| 53 | + function(f).swap(*this); |
| 54 | + return *this; |
| 55 | + } |
| 56 | + |
| 57 | + void swap(function& other) { |
| 58 | + invoker_base<R, Args...>* tmp = invoker; |
| 59 | + invoker = other.invoker; |
| 60 | + other.invoker = tmp; |
| 61 | + } |
| 62 | + |
| 63 | + result_type operator()(Args... args) const { |
| 64 | + return invoker->invoke(args...); |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + invoker_base<R, Args...>* invoker; |
| 69 | +}; |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +template<typename TemplateParam> |
| 74 | +struct Problem { |
| 75 | + template<typename FunctionTemplateParam> |
| 76 | + constexpr int FuncAlign(int param = alignof(FunctionTemplateParam)); |
| 77 | + |
| 78 | + template<typename FunctionTemplateParam> |
| 79 | + constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam)); |
| 80 | + |
| 81 | + template<typename FunctionTemplateParam> |
| 82 | + constexpr int FuncAlign2(int param = alignof(TemplateParam)); |
| 83 | + |
| 84 | + template<typename FunctionTemplateParam> |
| 85 | + constexpr int FuncSizeof2(int param = sizeof(TemplateParam)); |
| 86 | +}; |
| 87 | + |
| 88 | +template<typename TemplateParam> |
| 89 | +struct Problem<TemplateParam*> { |
| 90 | + template<typename FunctionTemplateParam> |
| 91 | + constexpr int FuncAlign(int param = alignof(FunctionTemplateParam)); |
| 92 | + |
| 93 | + template<typename FunctionTemplateParam> |
| 94 | + constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam)); |
| 95 | + |
| 96 | + template<typename FunctionTemplateParam> |
| 97 | + constexpr int FuncAlign2(int param = alignof(TemplateParam)); |
| 98 | + |
| 99 | + template<typename FunctionTemplateParam> |
| 100 | + constexpr int FuncSizeof2(int param = sizeof(TemplateParam)); |
| 101 | +}; |
| 102 | + |
| 103 | +template<typename TemplateParam> |
| 104 | +template<typename FunctionTemplateParam> |
| 105 | +constexpr int Problem<TemplateParam*>::FuncAlign(int param) { |
| 106 | + return 2U*param; |
| 107 | +} |
| 108 | + |
| 109 | +template<typename TemplateParam> |
| 110 | +template<typename FunctionTemplateParam> |
| 111 | +constexpr int Problem<TemplateParam*>::FuncSizeof(int param) { |
| 112 | + return 2U*param; |
| 113 | +} |
| 114 | + |
| 115 | +template<typename TemplateParam> |
| 116 | +template<typename FunctionTemplateParam> |
| 117 | +constexpr int Problem<TemplateParam*>::FuncAlign2(int param) { |
| 118 | + return 2U*param; |
| 119 | +} |
| 120 | + |
| 121 | +template<typename TemplateParam> |
| 122 | +template<typename FunctionTemplateParam> |
| 123 | +constexpr int Problem<TemplateParam*>::FuncSizeof2(int param) { |
| 124 | + return 2U*param; |
| 125 | +} |
| 126 | + |
| 127 | +template <> |
| 128 | +template<typename FunctionTemplateParam> |
| 129 | +constexpr int Problem<int>::FuncAlign(int param) { |
| 130 | + return param; |
| 131 | +} |
| 132 | + |
| 133 | +template <> |
| 134 | +template<typename FunctionTemplateParam> |
| 135 | +constexpr int Problem<int>::FuncSizeof(int param) { |
| 136 | + return param; |
| 137 | +} |
| 138 | + |
| 139 | +template <> |
| 140 | +template<typename FunctionTemplateParam> |
| 141 | +constexpr int Problem<int>::FuncAlign2(int param) { |
| 142 | + return param; |
| 143 | +} |
| 144 | + |
| 145 | +template <> |
| 146 | +template<typename FunctionTemplateParam> |
| 147 | +constexpr int Problem<int>::FuncSizeof2(int param) { |
| 148 | + return param; |
| 149 | +} |
| 150 | + |
| 151 | +void foo() { |
| 152 | + Problem<int> p = {}; |
| 153 | + static_assert(p.FuncAlign<char>() == alignof(char)); |
| 154 | + static_assert(p.FuncSizeof<char>() == sizeof(char)); |
| 155 | + static_assert(p.FuncAlign2<char>() == alignof(int)); |
| 156 | + static_assert(p.FuncSizeof2<char>() == sizeof(int)); |
| 157 | + Problem<short*> q = {}; |
| 158 | + static_assert(q.FuncAlign<char>() == 2U * alignof(char)); |
| 159 | + static_assert(q.FuncSizeof<char>() == 2U * sizeof(char)); |
| 160 | + static_assert(q.FuncAlign2<char>() == 2U *alignof(short)); |
| 161 | + static_assert(q.FuncSizeof2<char>() == 2U * sizeof(short)); |
| 162 | +} |
| 163 | + |
| 164 | +template <typename T> |
| 165 | +class A { |
| 166 | + public: |
| 167 | + void run( |
| 168 | + std::function<void(T&)> f1 = [](auto&&) {}, |
| 169 | + std::function<void(T&)> f2 = [](auto&&) {}); |
| 170 | + private: |
| 171 | + class Helper { |
| 172 | + public: |
| 173 | + explicit Helper(std::function<void(T&)> f2) : f2_(f2) {} |
| 174 | + std::function<void(T&)> f2_; |
| 175 | + }; |
| 176 | +}; |
| 177 | + |
| 178 | +template <typename T> |
| 179 | +void A<T>::run(std::function<void(T&)> f1, |
| 180 | + std::function<void(T&)> f2) { |
| 181 | + Helper h(f2); |
| 182 | +} |
| 183 | + |
| 184 | +struct B {}; |
| 185 | + |
| 186 | +int main() { |
| 187 | + A<B> a; |
| 188 | + a.run([&](auto& l) {}); |
| 189 | + return 0; |
| 190 | +} |
0 commit comments