File: default-parm-init.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (190 lines) | stat: -rw-r--r-- 4,800 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
// expected-no-diagnostics

namespace std {

template<typename Signature> class function;

template<typename R, typename... Args> class invoker_base {
public: 
  virtual ~invoker_base() { } 
  virtual R invoke(Args...) = 0; 
  virtual invoker_base* clone() = 0;
};

template<typename F, typename R, typename... Args> 
class functor_invoker : public invoker_base<R, Args...> {
public: 
  explicit functor_invoker(const F& f) : f(f) { } 
  R invoke(Args... args) { return f(args...); } 
  functor_invoker* clone() { return new functor_invoker(f); }

private:
  F f;
};

template<typename R, typename... Args>
class function<R (Args...)> {
public: 
  typedef R result_type;
  function() : invoker (0) { }
  function(const function& other) : invoker(0) { 
    if (other.invoker)
      invoker = other.invoker->clone();
  }

  template<typename F> function(const F& f) : invoker(0) {
    invoker = new functor_invoker<F, R, Args...>(f);
  }

  ~function() { 
    if (invoker)
      delete invoker;
  }

  function& operator=(const function& other) { 
    function(other).swap(*this); 
    return *this;
  }

  template<typename F> 
  function& operator=(const F& f) {
    function(f).swap(*this); 
    return *this;
  }

  void swap(function& other) { 
    invoker_base<R, Args...>* tmp = invoker; 
    invoker = other.invoker; 
    other.invoker = tmp;
  }

  result_type operator()(Args... args) const { 
    return invoker->invoke(args...);
  }

private: 
  invoker_base<R, Args...>* invoker;
};

}

template<typename TemplateParam>
struct Problem {
  template<typename FunctionTemplateParam>
  constexpr int FuncAlign(int param = alignof(FunctionTemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncAlign2(int param = alignof(TemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncSizeof2(int param = sizeof(TemplateParam));
};

template<typename TemplateParam>
struct Problem<TemplateParam*> {
  template<typename FunctionTemplateParam>
  constexpr int FuncAlign(int param = alignof(FunctionTemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncAlign2(int param = alignof(TemplateParam));

  template<typename FunctionTemplateParam>
  constexpr int FuncSizeof2(int param = sizeof(TemplateParam));
};

template<typename TemplateParam>
template<typename FunctionTemplateParam>
constexpr int Problem<TemplateParam*>::FuncAlign(int param) {
	return 2U*param;
}

template<typename TemplateParam>
template<typename FunctionTemplateParam>
constexpr int Problem<TemplateParam*>::FuncSizeof(int param) {
    return 2U*param;
}

template<typename TemplateParam>
template<typename FunctionTemplateParam>
constexpr int Problem<TemplateParam*>::FuncAlign2(int param) {
	return 2U*param;
}

template<typename TemplateParam>
template<typename FunctionTemplateParam>
constexpr int Problem<TemplateParam*>::FuncSizeof2(int param) {
	return 2U*param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncAlign(int param) {
	return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncSizeof(int param) {
	return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncAlign2(int param) {
	return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncSizeof2(int param) {
	return param;
}

void foo() {
    Problem<int> p = {};
    static_assert(p.FuncAlign<char>() == alignof(char));
    static_assert(p.FuncSizeof<char>() == sizeof(char));
    static_assert(p.FuncAlign2<char>() == alignof(int));
    static_assert(p.FuncSizeof2<char>() == sizeof(int));
    Problem<short*> q = {};
    static_assert(q.FuncAlign<char>() == 2U * alignof(char));
    static_assert(q.FuncSizeof<char>() == 2U * sizeof(char));
    static_assert(q.FuncAlign2<char>() == 2U *alignof(short));
    static_assert(q.FuncSizeof2<char>() == 2U * sizeof(short));
}

template <typename T>
class A {
 public:
  void run(
    std::function<void(T&)> f1 = [](auto&&) {},
    std::function<void(T&)> f2 = [](auto&&) {});
 private:
  class Helper {
   public:
    explicit Helper(std::function<void(T&)> f2) : f2_(f2) {}
    std::function<void(T&)> f2_;
  };
};

template <typename T>
void A<T>::run(std::function<void(T&)> f1,
               std::function<void(T&)> f2) {
  Helper h(f2);
}

struct B {};

int main() {
    A<B> a;
    a.run([&](auto& l) {});
    return 0;
}