• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/template_util.h"
6 
7 #include <string>
8 
9 #include "base/containers/flat_tree.h"
10 #include "base/test/move_only_int.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 namespace {
15 
16 enum SimpleEnum { SIMPLE_ENUM };
17 enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
18 enum class ScopedEnum { SCOPED_ENUM };
19 enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
operator <<(std::ostream & os,ScopedEnumWithOperator v)20 std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
21   return os;
22 }
23 struct SimpleStruct {};
24 struct StructWithOperator {};
operator <<(std::ostream & os,const StructWithOperator & v)25 std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
26   return os;
27 }
28 
29 // is_non_const_reference<Type>
30 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
31 static_assert(!is_non_const_reference<const int&>::value,
32               "IsNonConstReference");
33 static_assert(is_non_const_reference<int&>::value, "IsNonConstReference");
34 
35 // A few standard types that definitely support printing.
36 static_assert(internal::SupportsOstreamOperator<int>::value,
37               "ints should be printable");
38 static_assert(internal::SupportsOstreamOperator<const char*>::value,
39               "C strings should be printable");
40 static_assert(internal::SupportsOstreamOperator<std::string>::value,
41               "std::string should be printable");
42 
43 // Various kinds of enums operator<< support.
44 static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
45               "simple enum should be printable by value");
46 static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
47               "simple enum should be printable by const ref");
48 static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
49               "enum with explicit type should be printable by value");
50 static_assert(
51     internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
52     "enum with explicit type should be printable by const ref");
53 static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
54               "scoped enum should not be printable by value");
55 static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
56               "simple enum should not be printable by const ref");
57 static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
58               "scoped enum with operator<< should be printable by value");
59 static_assert(
60     internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
61     "scoped enum with operator<< should be printable by const ref");
62 
63 // operator<< support on structs.
64 static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
65               "simple struct should not be printable by value");
66 static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
67               "simple struct should not be printable by const ref");
68 static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
69               "struct with operator<< should be printable by value");
70 static_assert(
71     internal::SupportsOstreamOperator<const StructWithOperator&>::value,
72     "struct with operator<< should be printable by const ref");
73 
74 // base::is_trivially_copyable
75 class TrivialCopy {
76  public:
TrivialCopy(int d)77   TrivialCopy(int d) : data_(d) {}
78 
79  protected:
80   int data_;
81 };
82 
83 class TrivialCopyButWithDestructor : public TrivialCopy {
84  public:
TrivialCopyButWithDestructor(int d)85   TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
~TrivialCopyButWithDestructor()86   ~TrivialCopyButWithDestructor() { data_ = 0; }
87 };
88 
89 static_assert(base::is_trivially_copyable<TrivialCopy>::value,
90               "TrivialCopy should be detected as trivially copyable");
91 static_assert(!base::is_trivially_copyable<TrivialCopyButWithDestructor>::value,
92               "TrivialCopyButWithDestructor should not be detected as "
93               "trivially copyable");
94 
95 class NoCopy {
96  public:
97   NoCopy(const NoCopy&) = delete;
98 };
99 
100 static_assert(
101     !base::is_trivially_copy_constructible<std::vector<NoCopy>>::value,
102     "is_trivially_copy_constructible<std::vector<T>> must be compiled.");
103 
104 }  // namespace
105 
106 }  // namespace base
107