blob: 2c42445f785d87d6e570f6ad87d852b6dad59489 [file] [log] [blame]
[email protected]a6d6d4682012-06-19 18:34:081// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b38d3572011-02-15 01:27:382// 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"
[email protected]a6d6d4682012-06-19 18:34:086
jbroman6bcfec422016-05-26 00:28:467#include <string>
8
dyaroshev99a394d2017-07-22 19:10:189#include "base/containers/flat_tree.h"
10#include "base/test/move_only_int.h"
[email protected]b38d3572011-02-15 01:27:3811#include "testing/gtest/include/gtest/gtest.h"
12
13namespace base {
14namespace {
15
jbroman6bcfec422016-05-26 00:28:4616enum SimpleEnum { SIMPLE_ENUM };
17enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
18enum class ScopedEnum { SCOPED_ENUM };
19enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
20std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
21 return os;
22}
23struct SimpleStruct {};
24struct StructWithOperator {};
25std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
26 return os;
27}
28
[email protected]a6d6d4682012-06-19 18:34:0829// is_non_const_reference<Type>
avi4ec0dff2015-11-24 14:26:2430static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
31static_assert(!is_non_const_reference<const int&>::value,
32 "IsNonConstReference");
33static_assert(is_non_const_reference<int&>::value, "IsNonConstReference");
[email protected]b38d3572011-02-15 01:27:3834
jbroman6bcfec422016-05-26 00:28:4635// A few standard types that definitely support printing.
36static_assert(internal::SupportsOstreamOperator<int>::value,
37 "ints should be printable");
38static_assert(internal::SupportsOstreamOperator<const char*>::value,
39 "C strings should be printable");
40static_assert(internal::SupportsOstreamOperator<std::string>::value,
41 "std::string should be printable");
42
43// Various kinds of enums operator<< support.
44static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
45 "simple enum should be printable by value");
46static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
47 "simple enum should be printable by const ref");
48static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
49 "enum with explicit type should be printable by value");
50static_assert(
51 internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
52 "enum with explicit type should be printable by const ref");
53static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
54 "scoped enum should not be printable by value");
55static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
56 "simple enum should not be printable by const ref");
57static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
58 "scoped enum with operator<< should be printable by value");
59static_assert(
60 internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
61 "scoped enum with operator<< should be printable by const ref");
62
63// operator<< support on structs.
64static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
65 "simple struct should not be printable by value");
66static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
67 "simple struct should not be printable by const ref");
68static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
69 "struct with operator<< should be printable by value");
70static_assert(
71 internal::SupportsOstreamOperator<const StructWithOperator&>::value,
72 "struct with operator<< should be printable by const ref");
73
Brett Wilson4208cb52017-07-17 20:52:3074// base::is_trivially_copyable
75class TrivialCopy {
76 public:
77 TrivialCopy(int d) : data_(d) {}
78
79 protected:
80 int data_;
81};
82
83class TrivialCopyButWithDestructor : public TrivialCopy {
84 public:
85 TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
86 ~TrivialCopyButWithDestructor() { data_ = 0; }
87};
88
89static_assert(base::is_trivially_copyable<TrivialCopy>::value,
90 "TrivialCopy should be detected as trivially copyable");
91static_assert(!base::is_trivially_copyable<TrivialCopyButWithDestructor>::value,
92 "TrivialCopyButWithDestructor should not be detected as "
93 "trivially copyable");
94
Hidehiko Abe4d8468a2018-02-23 09:50:4195class NoCopy {
96 public:
97 NoCopy(const NoCopy&) = delete;
98};
99
100static_assert(
101 !base::is_trivially_copy_constructible<std::vector<NoCopy>>::value,
102 "is_trivially_copy_constructible<std::vector<T>> must be compiled.");
103
[email protected]b38d3572011-02-15 01:27:38104} // namespace
dyaroshev99a394d2017-07-22 19:10:18105
[email protected]b38d3572011-02-15 01:27:38106} // namespace base