is it possible to do a nullable call in cpp

in kotlin, it is very common to do a nullable call like

taskQueryDto.entity?.resolveVersions?.isMember(root, criteriaBuilder, predicates, "resolveVersions")

and in js flow, it is common too

const versionIndex = this.state.task.resolveVersions?.findIndex(value => value.id === resolveVersion.id)

nullable call which allow u call a function without worry about nullptr is very useful and common in other languages, but in cpp, it seems a long long way to go, what i can find is absolute no, or a big big micro which knock me down when it comes to my eyes.

i mean, will ue kindly enough to make some progress on nullable call?

if your goal is to figure out if a pointer is null then you can just outright check for that:

AMyActor* myActor = GetMyActorFromContainer();
if(myActor) { // the more verbose version of if(myActor != nullptr) would also work
// if you are going to specify a test or assignment of null then it is advised to use 'nullptr' rather then 'NULL' for platform compatibility.

if you prefer the notation you could even put your null check into a ternary expression

int32 MyInt = (myActor != nullptr) ? myActor->GetSpecialInt() : 0;

although if you would be accessing multiple things, or if the pointer being null would be “very bad” then you might want to put the check in either an early re-acquire, or early return.

if the function shall return a pointer similar to the above you can wrap the declaration into the if statement which can be used for scope locking the pointer as well to prevent trying to access the pointer outside of scope if the assignment failed

if( AMyActor* myActor = GetMyActorFromContainer() ){

if the pointer is being assigned/returned as one of the arguments to the function then you should manually check the pointer you are about to access.

Many of the Engines provided functions that involve getting a pointer will often instead have bool or int32 as the return type, and the pointer as an argument, so if the function returns false, or INDEX_NONE then the pointer will most likely be null.

because C++ is fully built realizing that pointers can be null it is almost boiler plate to unless you are creating it right there and then; Then please check them against null. and please don’t do “pointer magic”, or assume the size of a object (even down to int and char)


both of these languages had these nullable calls as a way to “clean up the code” and to “protect the programmer from themselves” in a sense, C++ in many ways has FAR fewer guard rails then most other “High level languages”, but it also offers a lot more power and control, like how Java does its level best to hide pointers away, and utilize “Truthiness” for comparisons.

yeah, nullable function call is very good practice

in some way, i think cpp can have this feature, because it is not syntax, it is pre-compiled to standard corresponding cpp code before the real compile begin.

like the micros, they are not existing on the compile time, they are compiled before the compile phase.

maybe not that clear, sorry

then your request is not for Unreal, but for the C++ maintainers (the C++ standards Committee). keep in mind that there is a lag time between the maintainers defining a new specification (about every 3 years so the next one should be C++26), and it being implemented into the latest version of the compilers, often with about a 3-6 year gap between the standard being published and it being implemented into the compilers, though it is in the best interest of the compiler’s relevance to implement the features in a “reasonable” amount of time

the “closest” you get is having the function return whether or not the pointer will be pointing to null or pointing to the Heap, or wrapping the pointers declaration and assignment into a single if scope.

Java is sometimes called “syntactic sugar on C++” (I personally don’t have experience with Kotlin, so I don’t know its absolute relation and build up from languages like C++), and even then these “nullable calls” are functionally just syntax of putting a comparison to null with the assignment, it does it with extra symbols instead of potentially additional scope and nesting.

bool FuncDoStuff(MyPointer* inPointer){
   if (inPointer == nullptr) { return false; }
   // do some other stuff
   return true;
}

with C++98(auto pointers, smart pointers), C++ 17 (optional) and C++ 20 (Spaceship operator <=>) have introduced mechanisms for things similar to nullable calls, but these are are reliant on smart pointers, which unreal does wrap on for the specialized pointers we are given. but we are advised for return types and parameters to use raw pointers and references especially for blueprint facing code.

for the question that you asked “is it possible to do a nullable call in cpp?”
technically yes, but it just takes wrapping it in an if block, or relying on the return value of the function.

things like

if( UMyWorldSubsystem* system = GetWorld()->GetSubsystem<UMyWorldSubsystem>() ){
    // if the subsystem exists then it will be assigned to the pointer 'system' and safe to access
    // it will also only be accessible within this if block for additional safty
} else {
    // the subsystem does not exist currently
}

or

bool LineTraceWrap(FVector Start, FVector End) {
    FHitRestult HitResult;
    if( GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility) ) {
        // something was hit on that channel either blocking or overlapping, and it is "safe" to try and work with the pointers in the 'FHitResult'
    } else {
        // nothing was hit do something if you need.
    }
}

the big reason why nullable calls have not been implemented is because in normal C++ we don’t have garbage collection, so the only way the pointer will be freed or deleted is if that operation is called explicitly.
for example there are situations in Java where if memory is being consumed too rapidly and malloc is unable to return additional addresses then a GC will be activated, and that pointer you are actively working on might be just gone, while you are working on it; leading to a rare case of Null_Reference_Exception in Java (unlikely but is not impossible, although it is equally possible that the runtime will return the value at the offset but this has additional problems)

thanks, i guess i have to accept the current if block format and for a long time. and today i have changed my ide from Visual Studio to Rider, which helps a lot, i think i will not have a strong willing on nullable function call for a while, at least before i get stuck on Rider.

anyway, thanks

Luckily we have the full arsenal of templates and macros available, so we don’t have to wait years for the CPP committee and then more time for compiler developers to add a feature to the language.

Sure having a native operator built into the language would be nicer but heres a quick example I put together that gives you a NULLABLE_CALL macro that takes the pointer as first parameter, the function to be called as second and any additional function parameters required (if any) via variadic macro parameters: Compiler Explorer

You could of course just hide the ?: in the macro as well 


auto resolveVersions = NULLABLE_CALL(taskQueryDto.entity, getResolveVersions);
bool isMember = NULLABLE_CALL(resolveVersions, isMember, root, criteriaBuilder, predicates, "resolveVersions");
1 Like

wow, u are the best, thank u very much