Can a function be const but not pure?

Hello,

I had some functions that are const (since they, indeed, do not affect the object) but do a lot of work.
I set the BlueprintCallable flag in the UPROPERTY but it seems that the Blueprint system force the transformation in “pure” function.

But here is the problem :

Pure Functions are wired to Data Pins
and are automatically executed by the
compiler when the data relying on them
is required. This means that a Pure
Function will be called one time for
each node it is connected to.

Source : Functions | Unreal Engine Documentation

For these “heavy” functions, it will be better for me to have an impure function to be sure that the function is not called several time. I could use local variables to stock the result (even multiple results) but it will be very inconvenient…

So, my question is : is there a work around to force the use of impure functions in Blueprint, keeping the ‘const’ ?

Thanks for reading !

This may help you

Look like there no way to avoid it, here code reponcible for this behavior, it also has marked todo and it might change in the future so this is atleast optional:

// @todo: the presence of const and one or more outputs does not guarantee that there are
// no side effects. On GCC and clang we could use __attribure__((pure)) or __attribute__((const))
// or we could just rely on the use marking things BlueprintPure. Either way, checking the C++
// const identifier to determine purity is not desirable. We should remove the following logic:

// If its a const BlueprintCallable function with some sort of output, mark it as BlueprintPure as well
if ( bHasAnyOutputs && ((FuncInfo.FunctionFlags & FUNC_BlueprintCallable) != 0) )
{
	FuncInfo.FunctionFlags |= FUNC_BlueprintPure;
}

So for now there no work around, or you make you function non-const or don’t have outputs (as you can see in code it also checks that)

You can also modify this code (by removing that part), it’s inside UHT source code and function is marked as pure before compilation during parse of header files, so it’s safer then modifying engine code. Here location of that code:

https://github.com/EpicGames/UnrealEngine/blob/ec89afdca643efec6aaa1badc5b2b07df27eaec3/Engine/Source/Programs/UnrealHeaderTool/Private/HeaderParser.cpp#L5353

1 Like

Thank you very much ! I will use local variables so ^^

Thank you too, I didn’t found this post earlier. I found some other posts about that question but didn’t purpose a work around or something. Just “When it’s const, it will be pure. End.”

An update for this question: This has been possible since 4.12 with BlueprintPure=false (see the answer here for an example: BlueprintePure=False metadata not working as expected - Blueprint - Unreal Engine Forums)