xref: /aosp_15_r20/external/cronet/base/callback_list_nocompile.nc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2013 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include "base/callback_list.h"
9
10#include <memory>
11#include <utility>
12
13#include "base/functional/bind.h"
14#include "base/functional/callback_helpers.h"
15
16namespace base {
17
18class Foo {
19 public:
20  Foo() {}
21  ~Foo() {}
22};
23
24class FooListener {
25 public:
26  FooListener() = default;
27  FooListener(const FooListener&) = delete;
28  FooListener& operator=(const FooListener&) = delete;
29
30  void GotAScopedFoo(std::unique_ptr<Foo> f) { foo_ = std::move(f); }
31
32  std::unique_ptr<Foo> foo_;
33};
34
35// Callbacks run with a move-only typed parameter.
36//
37// CallbackList does not support move-only typed parameters. Notify() is
38// designed to take zero or more parameters, and run each registered callback
39// with them. With move-only types, the parameter will be set to NULL after the
40// first callback has been run.
41void WontCompile() {
42  FooListener f;
43  RepeatingCallbackList<void(std::unique_ptr<Foo>)> c1;
44  CallbackListSubscription sub =
45      c1.Add(BindRepeating(&FooListener::GotAScopedFoo, Unretained(&f)));
46  c1.Notify(std::unique_ptr<Foo>(new Foo()));  // expected-error@*:* {{call to implicitly-deleted copy constructor of 'std::unique_ptr<base::Foo>'}}
47}
48
49}  // namespace base
50