#include <folly/Traits.h>
#include <folly/Unit.h>
#include <folly/Utility.h>
+#include <folly/lang/ColdClass.h>
#define FOLLY_EXPECTED_ID(X) FB_CONCATENATE(FB_CONCATENATE(Folly, X), __LINE__)
* Expected objects in the error state.
*/
template <class Error>
-class Unexpected final {
+class Unexpected final : ColdClass {
template <class E>
friend class Unexpected;
template <class V, class E>
Unexpected(Unexpected&&) = default;
Unexpected& operator=(const Unexpected&) = default;
Unexpected& operator=(Unexpected&&) = default;
- FOLLY_COLD constexpr /* implicit */ Unexpected(const Error& err)
- : error_(err) {}
- FOLLY_COLD constexpr /* implicit */ Unexpected(Error&& err)
- : error_(std::move(err)) {}
+ constexpr /* implicit */ Unexpected(const Error& err) : error_(err) {}
+ constexpr /* implicit */ Unexpected(Error&& err) : error_(std::move(err)) {}
template <class Other FOLLY_REQUIRES_TRAILING(
std::is_constructible<Error, Other&&>::value)>
io/async/test/Util.h \
json.h \
lang/Assume.h \
+ lang/ColdClass.h \
lang/Launder.h \
lang/RValueReferenceWrapper.h \
lang/SafeAssert.h \
io/async/ssl/SSLErrors.cpp \
json.cpp \
lang/Assume.cpp \
+ lang/ColdClass.cpp \
lang/SafeAssert.cpp \
detail/MemoryIdler.cpp \
detail/SocketFastOpen.cpp \
--- /dev/null
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "ColdClass.h"
+
+folly::cold_detail::ColdClass::ColdClass() noexcept {}
--- /dev/null
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Tag any class as `cold` by inheriting from folly::cold::ColdClass
+ *
+ * Intended use: things like folly::Unexpected which are designed to only be
+ * instantiated on error paths.
+ */
+#pragma once
+
+#include <folly/CppAttributes.h>
+
+namespace folly {
+// ColdClass should be in its own namespace: inheriting from any class adds its
+// innermost namespace to the namespaces inspected during
+// argument-dependent-lookoup. We want people to be able to derive from this
+// without implicitly picking up the folly namespace for ADL on their classes.
+namespace cold_detail {
+struct ColdClass {
+ FOLLY_COLD ColdClass() noexcept;
+};
+} // namespace cold
+
+/* using override */ using cold_detail::ColdClass;
+} // namespace folly
--- /dev/null
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/lang/ColdClass.h>
+
+#include <folly/portability/GTest.h>
+#include <type_traits>
+
+using folly::ColdClass;
+
+TEST(ColdClass, inheritance) {
+ // The only verifiable property of ColdClass is that it must not disrupt the
+ // default constructor/destructor, default copy/move constructors and default
+ // copy/move assignment operators when a class derives from it.
+ struct TestStruct : ColdClass {};
+ EXPECT_TRUE(std::is_nothrow_default_constructible<TestStruct>::value);
+ EXPECT_TRUE(std::is_trivially_copy_constructible<TestStruct>::value);
+ EXPECT_TRUE(std::is_trivially_move_constructible<TestStruct>::value);
+ EXPECT_TRUE(std::is_trivially_copy_assignable<TestStruct>::value);
+ EXPECT_TRUE(std::is_trivially_move_assignable<TestStruct>::value);
+ EXPECT_TRUE(std::is_trivially_destructible<TestStruct>::value);
+ // Same again, but private inheritance. Should make no difference.
+ class TestClass : ColdClass {};
+ EXPECT_TRUE(std::is_nothrow_default_constructible<TestClass>::value);
+ EXPECT_TRUE(std::is_trivially_copy_constructible<TestClass>::value);
+ EXPECT_TRUE(std::is_trivially_move_constructible<TestClass>::value);
+ EXPECT_TRUE(std::is_trivially_copy_assignable<TestClass>::value);
+ EXPECT_TRUE(std::is_trivially_move_assignable<TestClass>::value);
+ EXPECT_TRUE(std::is_trivially_destructible<TestClass>::value);
+}