-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing buffer_appender's ++ slicing. #1822
Conversation
buffer_appender tmp = *this; | ||
++*this; | ||
return tmp; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that back_insert_iterator
only has like... 4 member functions, and I've already implemented 2 of them (and over-implemented them too, these could both just be return *this;
), maybe we should just implement the whole thing in here without std
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Looks good but I suggest simplifying the test case and making it more portable.
test/format-test.cc
Outdated
@@ -2473,3 +2473,37 @@ TEST(FormatTest, FormatUTF8Precision) { | |||
EXPECT_EQ(result.size(), 5); | |||
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5))); | |||
} | |||
|
|||
#ifdef __cpp_decltype_auto |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the test work without this, e.g. checking if the types are the same manually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Just have a static_assert
.
test/format-test.cc
Outdated
struct lazy_optional { | ||
bool engaged; | ||
std::string value; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd minimize this even further into an empty struct and make the name reflect what we are trying to test here.
buffer_appender tmp = *this; | ||
++*this; | ||
return tmp; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine as is.
test/format-test.cc
Outdated
auto format(check_back_appender, Context& ctx) -> decltype(ctx.out()) { | ||
// needs to satisfy weakly_incrementable | ||
auto out = ctx.out(); | ||
static_assert(std::is_same<decltype(++out), decltype(out)&>::value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a message to fix contbuild.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, done.
Thanks! |
buffer_appender
's pre- and post-fix increment operators slice down toback_inserter
, which means it actually breaks the iterator requirements (which would be actually checked in C++20, it is notweakly_incrementable
) and also breaks any code that assumes that++it
andit
have the same type (as illustrated in the test case, a reduced form of how we format our optional type, which no longer compiles).