-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[BUG] in
parameter optimization should be less observable
#686
Comments
Both in this issue, and #250 , problems arise due to functions returning a reference. What if the semantics were that optimization is only applied when result is returned by value and otherwise, a |
That's just an easy way to manifest the difference in optimization. |
Correct me if I'm wrong, but in that case, a copy vs reference won't make a difference, right? |
The difference is the validity of the address. |
It doesn't make a difference if you store the parameter.
Something like this (https://cpp2.godbolt.org/z/9Yhfc351r):
No warnings, but the last assertion passes.
|
Thanks for the example! While my point 2) in #696 does solve the problem, it will require rewriting the example to some other form (possibly taking in pointers as parameters) which is not desirable. |
I'm struggling with that particular example. The program is wrong, of course, but making template <typename T>
game_map::add_entity(const T& x) {
entities.push_back(&x);
} instead of template <typename T>
game_map::add_entity(T x) {
entities.push_back(&x);
} doesn't fix it. You can still pass a temporary to If I want to express "pass me an object whose address outlives me", I can (and should) do: game_map: @struct type = {
entities: std::vector<* const void>;
add_entity: (inout this, x: *_) = entities.push_back(x);
} no? |
Yes.
|
Title:
in
parameter optimization should be less observable.Description:
Some
in
parameters are optimized to pass-by-value.This can result in code that inadvertently depends on the optimization or lack thereof.
Minimal reproducer (https://cpp2.godbolt.org/z/nWKea3xGx):
Commands:
cppfront main.cpp2 clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -I . main.cpp
Expected result:
More consistent results between the operations done on
in
parameters regardless of optimization.This overload
f
has different semantics for optimized and non-optimizedin
parameters.The overload could be called
address
,in which case it's very obviously wrong for
int
,but very much passes for
std::array<int, 42>
in today's Cppfront,even though the meaning could change for a lower number of elements.
Actual result and error:
Cpp2 lowered to Cpp1:
The text was updated successfully, but these errors were encountered: