Skip to content

Commit 25cb08b

Browse files
Optimization does not use a "caching iterator"
1 parent 2a285e4 commit 25cb08b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/beman/any_view/constexpr.test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,26 @@ TEST(ConstexprTest, sort_vector) {
6464
#endif
6565
EXPECT_TRUE(sort(std::vector{6, 8, 7, 5, 3, 0, 9}));
6666
}
67+
68+
constexpr auto set_front(any_view<int, forward> view, int value) {
69+
// forward iterator of lvalue reference uses cache object to fuse virtual dispatches
70+
#ifndef _MSC_VER
71+
// TODO use [[msvc::no_unique_address]]
72+
static_assert(sizeof(std::ranges::iterator_t<any_view<int, forward>>) ==
73+
sizeof(std::ranges::iterator_t<any_view<int, input>>) + sizeof(int*));
74+
#endif
75+
76+
auto& ref = view.front();
77+
// even with cache object, lifetime of reference is not tied to lifetime of iterator
78+
ref = value;
79+
80+
return view;
81+
}
82+
83+
TEST(ConstexprTest, reference_lifetime) {
84+
#ifndef _MSC_VER
85+
// error C2131: expression did not evaluate to a constant
86+
static_assert(42 == set_front(std::vector{7}, 42).front());
87+
#endif
88+
EXPECT_EQ(42, set_front(std::vector{7}, 42).front());
89+
}

0 commit comments

Comments
 (0)