• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2024 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/containers/adapters.h"
9#include "base/containers/span.h"
10
11#include <utility>
12#include <vector>
13
14namespace base {
15
16void DanglingInRangeBasedForLoop() {
17  // This is not safe prior to C++23, since the temporary vector does not have
18  // its lifetime extended.
19  for (int&& x : RangeAsRvalues(std::vector({1, 2, 3}))) {  // expected-error {{temporary implicitly bound to local reference will be destroyed at the end of the full-expression}}
20    x *= x;
21  }
22  for (int& x : Reversed(std::vector({1, 2, 3}))) {  // expected-error {{temporary implicitly bound to local reference will be destroyed at the end of the full-expression}}
23    x *= x;
24  }
25}
26
27void RangeAsRvaluesRequiresNonBorrowedRange() {
28  std::vector<int> v;
29  RangeAsRvalues(v);  // expected-error {{no matching function for call to 'RangeAsRvalues'}}
30}
31
32void RangeAsRvaluesRequiresMutableRange() {
33  // A non-mutable range can't be moved from.
34  const std::vector<int> v;
35  RangeAsRvalues(std::move(v));  // expected-error {{no matching function for call to 'RangeAsRvalues'}}
36}
37
38}  // namespace base
39