• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 #ifndef BASE_TYPES_ALWAYS_FALSE_H_
6 #define BASE_TYPES_ALWAYS_FALSE_H_
7 
8 namespace base {
9 
10 // A helper that can be used with a static_assert() that must always fail (e.g.
11 // for an undesirable template instantiation). Such a static_assert() cannot
12 // simply be written as static_assert(false, ...) because that would always fail
13 // to compile, even if the template was never instantiated. Instead, a common
14 // idiom is to force the static_assert() to depend on a template parameter so
15 // that it is only evaluated when the template is instantiated:
16 //
17 // template <typename U = T>
18 // void SomeDangerousMethodThatShouldNeverCompile() {
19 //   static_assert(base::AlwaysFalse<U>, "explanatory message here");
20 // }
21 
22 namespace internal {
23 
24 template <typename... Args>
25 struct AlwaysFalseHelper {
26   static constexpr bool kValue = false;
27 };
28 
29 }  // namespace internal
30 
31 template <typename... Args>
32 inline constexpr bool AlwaysFalse =
33     internal::AlwaysFalseHelper<Args...>::kValue;
34 
35 }  // namespace base
36 
37 #endif  // BASE_TYPES_ALWAYS_FALSE_H_
38