David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "base/containers/flat_tree.h" |
| 10 | #include "base/test/move_only_int.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace base { |
| 14 | namespace { |
| 15 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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 }; |
| 20 | std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) { |
| 21 | return os; |
| 22 | } |
| 23 | struct SimpleStruct {}; |
| 24 | struct StructWithOperator {}; |
| 25 | std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) { |
| 26 | return os; |
| 27 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 28 | |
| 29 | // is_non_const_reference<Type> |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 34 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 42 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 62 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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"); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 73 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 74 | // base::is_trivially_copyable |
| 75 | class TrivialCopy { |
| 76 | public: |
| 77 | TrivialCopy(int d) : data_(d) {} |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 78 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 79 | protected: |
| 80 | int data_; |
| 81 | }; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 82 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 83 | class TrivialCopyButWithDestructor : public TrivialCopy { |
| 84 | public: |
| 85 | TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {} |
| 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."); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 103 | |
| 104 | } // namespace |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 105 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 106 | } // namespace base |