Skip to content

Commit 56c5fc6

Browse files
ezbrcopybara-github
authored andcommitted
Introduce IfConstexpr(Else) utilities for use internally in abseil library implementation.
These functions are meant to allow for avoiding complex SFINAE pre-C++17. PiperOrigin-RevId: 527968428 Change-Id: I6524206c9dc50663d7c38abf6d0e8080ca93fc30
1 parent 65109ec commit 56c5fc6

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

CMake/AbseilDll.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ set(ABSL_INTERNAL_DLL_FILES
430430
"types/span.h"
431431
"types/internal/span.h"
432432
"types/variant.h"
433+
"utility/internal/if_constexpr.h"
433434
"utility/utility.h"
434435
"debugging/leak_check.cc"
435436
)

absl/utility/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,26 @@ cc_test(
5252
"@com_google_googletest//:gtest_main",
5353
],
5454
)
55+
56+
cc_library(
57+
name = "if_constexpr",
58+
hdrs = [
59+
"internal/if_constexpr.h",
60+
],
61+
copts = ABSL_DEFAULT_COPTS,
62+
linkopts = ABSL_DEFAULT_LINKOPTS,
63+
deps = [
64+
"//absl/base:config",
65+
],
66+
)
67+
68+
cc_test(
69+
name = "if_constexpr_test",
70+
srcs = ["internal/if_constexpr_test.cc"],
71+
copts = ABSL_TEST_COPTS,
72+
linkopts = ABSL_DEFAULT_LINKOPTS,
73+
deps = [
74+
":if_constexpr",
75+
"@com_google_googletest//:gtest_main",
76+
],
77+
)

absl/utility/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,27 @@ absl_cc_test(
4242
absl::strings
4343
GTest::gmock_main
4444
)
45+
46+
absl_cc_library(
47+
NAME
48+
if_constexpr
49+
HDRS
50+
"internal/if_constexpr.h"
51+
COPTS
52+
${ABSL_DEFAULT_COPTS}
53+
DEPS
54+
absl::config
55+
PUBLIC
56+
)
57+
58+
absl_cc_test(
59+
NAME
60+
if_constexpr_test
61+
SRCS
62+
"internal/if_constexpr_test.cc"
63+
COPTS
64+
${ABSL_TEST_COPTS}
65+
DEPS
66+
absl::if_constexpr
67+
GTest::gmock_main
68+
)

absl/utility/internal/if_constexpr.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2023 The Abseil Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// The IfConstexpr and IfConstexprElse utilities in this file are meant to be
16+
// used to emulate `if constexpr` in pre-C++17 mode in library implementation.
17+
// The motivation is to allow for avoiding complex SFINAE.
18+
//
19+
// The functions passed in must depend on the type(s) of the object(s) that
20+
// require SFINAE. For example:
21+
// template<typename T>
22+
// int MaybeFoo(T& t) {
23+
// if constexpr (HasFoo<T>::value) return t.foo();
24+
// return 0;
25+
// }
26+
//
27+
// can be written in pre-C++17 as:
28+
//
29+
// template<typename T>
30+
// int MaybeFoo(T& t) {
31+
// int i = 0;
32+
// absl::utility_internal::IfConstexpr<HasFoo<T>::value>(
33+
// [&](const auto& fooer) { i = fooer.foo(); }, t);
34+
// return i;
35+
// }
36+
37+
#ifndef ABSL_UTILITY_INTERNAL_IF_CONSTEXPR_H_
38+
#define ABSL_UTILITY_INTERNAL_IF_CONSTEXPR_H_
39+
40+
#include <tuple>
41+
#include <utility>
42+
43+
#include "absl/base/config.h"
44+
45+
namespace absl {
46+
ABSL_NAMESPACE_BEGIN
47+
48+
namespace utility_internal {
49+
50+
template <bool condition, typename TrueFunc, typename FalseFunc,
51+
typename... Args>
52+
auto IfConstexprElse(TrueFunc&& true_func, FalseFunc&& false_func,
53+
Args&&... args) {
54+
return std::get<condition>(std::forward_as_tuple(
55+
std::forward<FalseFunc>(false_func), std::forward<TrueFunc>(true_func)))(
56+
std::forward<Args>(args)...);
57+
}
58+
59+
template <bool condition, typename Func, typename... Args>
60+
void IfConstexpr(Func&& func, Args&&... args) {
61+
IfConstexprElse<condition>(std::forward<Func>(func), [](auto&&...){},
62+
std::forward<Args>(args)...);
63+
}
64+
65+
} // namespace utility_internal
66+
67+
ABSL_NAMESPACE_END
68+
} // namespace absl
69+
70+
#endif // ABSL_UTILITY_INTERNAL_IF_CONSTEXPR_H_
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2023 The Abseil Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "absl/utility/internal/if_constexpr.h"
16+
17+
#include <utility>
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace {
22+
23+
struct Empty {};
24+
struct HasFoo {
25+
int foo() const { return 1; }
26+
};
27+
28+
TEST(IfConstexpr, Basic) {
29+
int i = 0;
30+
absl::utility_internal::IfConstexpr<false>(
31+
[&](const auto& t) { i = t.foo(); }, Empty{});
32+
EXPECT_EQ(i, 0);
33+
34+
absl::utility_internal::IfConstexpr<false>(
35+
[&](const auto& t) { i = t.foo(); }, HasFoo{});
36+
EXPECT_EQ(i, 0);
37+
38+
absl::utility_internal::IfConstexpr<true>(
39+
[&](const auto& t) { i = t.foo(); }, HasFoo{});
40+
EXPECT_EQ(i, 1);
41+
}
42+
43+
TEST(IfConstexprElse, Basic) {
44+
EXPECT_EQ(absl::utility_internal::IfConstexprElse<false>(
45+
[&](const auto& t) { return t.foo(); }, [&](const auto&) { return 2; },
46+
Empty{}), 2);
47+
48+
EXPECT_EQ(absl::utility_internal::IfConstexprElse<false>(
49+
[&](const auto& t) { return t.foo(); }, [&](const auto&) { return 2; },
50+
HasFoo{}), 2);
51+
52+
EXPECT_EQ(absl::utility_internal::IfConstexprElse<true>(
53+
[&](const auto& t) { return t.foo(); }, [&](const auto&) { return 2; },
54+
HasFoo{}), 1);
55+
}
56+
57+
struct HasFooRValue {
58+
int foo() && { return 1; }
59+
};
60+
struct RValueFunc {
61+
void operator()(HasFooRValue&& t) && { *i = std::move(t).foo(); }
62+
63+
int* i = nullptr;
64+
};
65+
66+
TEST(IfConstexpr, RValues) {
67+
int i = 0;
68+
RValueFunc func = {&i};
69+
absl::utility_internal::IfConstexpr<false>(
70+
std::move(func), HasFooRValue{});
71+
EXPECT_EQ(i, 0);
72+
73+
func = RValueFunc{&i};
74+
absl::utility_internal::IfConstexpr<true>(
75+
std::move(func), HasFooRValue{});
76+
EXPECT_EQ(i, 1);
77+
}
78+
79+
} // namespace

0 commit comments

Comments
 (0)