Skip to content

Conversation

@SekaiArendelle
Copy link

  • which help to some static analysis

- which help to some static analysis
@trcrsired
Copy link
Member

are you sure this works? list root can change, it would just produce wrong results

@SekaiArendelle
Copy link
Author

apologizing for misunderstanding, gnu::pure should be the right one.

@SekaiArendelle SekaiArendelle changed the title mark gnu::const to some method mark gnu::pure to some const method Nov 3, 2025
@SekaiArendelle
Copy link
Author

@trcrsired plz review again

@trcrsired
Copy link
Member

it is not pure either. allocation is not pure

@trcrsired
Copy link
Member

this PR is not correct. or i should say fundamentally wrong.

@trcrsired
Copy link
Member

pure function means:

y=f(x)

Any x must produce the same y. Here it is clearly not the case.

@SekaiArendelle
Copy link
Author

Cause C++ is not a functional programming language, gnu::pure should mean

  1. the output only rely on the input parameter (✓ this pointer)
  2. no side effect occurred.

@trcrsired
Copy link
Member

how? malloc is not deterministic and it affects on pointer

@SekaiArendelle
Copy link
Author

SekaiArendelle commented Nov 4, 2025

Why empty() and size() contains side effects? They did not edit anything except generating the output.

@SekaiArendelle
Copy link
Author

Why you consider bool empty(Class const*) and size_t size(Container const*) aren't pure function?

@SekaiArendelle
Copy link
Author

SekaiArendelle commented Nov 4, 2025

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, size and empty are definitely pure, without side effect.

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
}

@SekaiArendelle
Copy link
Author

@trcrsired when can this pr be merged?

@trcrsired
Copy link
Member

Can you go LLVM and ask LLVM folks on whether it is correct to add pure here first? thanks

@SekaiArendelle
Copy link
Author

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

However, the caller may safely change the contents of the array between successive calls to the function (doing so disables the optimization). The restriction also applies to member objects referenced by the this pointer in C++ non-static member functions.

See also:
https://clang.llvm.org/docs/AttributeReference.html#pure
Bad news! Clang does not document anything other than the title ) :

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?

@MacroModel
Copy link
Collaborator

MacroModel commented Dec 19, 2025

pure是指参数是指针然后回退相同内存访问的东西输出相同的结果,而const则是对指针的值一样就是一样的结果。pure可以用于base64加密这种东西,而const则完全依赖参数的(对应的值),其他都没法使用。

也就是说访问的内存值变量,但是地址(参数)不变。pure会重新计算,但const不会重新计算(尝试复用)

@trcrsired
Copy link
Member

add more tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants