mark gnu::pure to some const method#1213
mark gnu::pure to some const method#1213SekaiArendelle wants to merge 2 commits intocppfastio:nextfrom
Conversation
SekaiArendelle
commented
Nov 3, 2025
- which help to some static analysis
- which help to some static analysis
|
are you sure this works? list root can change, it would just produce wrong results |
|
apologizing for misunderstanding, gnu::pure should be the right one. |
|
@trcrsired plz review again |
|
it is not pure either. allocation is not pure |
|
this PR is not correct. or i should say fundamentally wrong. |
|
pure function means: y=f(x) Any x must produce the same y. Here it is clearly not the case. |
|
Cause C++ is not a functional programming language, gnu::pure should mean
|
|
how? malloc is not deterministic and it affects on pointer |
|
Why empty() and size() contains side effects? They did not edit anything except generating the output. |
|
Why you consider |
|
Basically, C++ is not a functional programming language and one key reason is that variables can be mutable, which can lead to things like: int plus(int a) {
return a+1;
}
int main() {
int a{};
a = plus(a);
plus(a); // same variable but different result
}Anyway, pure function for C++ can be explained as given the same STATUS (not the same variable) always produces same result. With above concept, int main() {
fast_io::string str{};
str.size(); // no side effect
str.append("text"); // status changed
str.size(); // like above example, same variable but not same status, is actually different input for pure function
} |
|
@trcrsired when can this pr be merged? |
|
Can you go LLVM and ask LLVM folks on whether it is correct to add pure here first? thanks |
|
Hi, I have investigated lots of stuff and I'm pretty ensure it's correct. See GCC Document: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
See also: To avoid (potential) off-topic discuss on llvm discord/issue, I asked this question to ykiko&Kimiv2&Another C++ community on discord, and the answer is yes. My final prove is https://github.com/llvm/llvm-project/blob/3a8f6979cef26ceb5ef5c6b8dba1fc1fd770c44c/clang/lib/AST/Expr.cpp#L3750 of its influence on side effect. Is above enough? |
|
pure是指参数是指针然后回退相同内存访问的东西输出相同的结果,而const则是对指针的值一样就是一样的结果。pure可以用于base64加密这种东西,而const则完全依赖参数的(对应的值),其他都没法使用。 也就是说访问的内存值变量,但是地址(参数)不变。pure会重新计算,但const不会重新计算(尝试复用) |
|
add more tests |