How to automatically coerce/infer type as FString?

I have a custom log function:

void ASagittarius::LogError(const FString& Msg)
{
	if (SagLogLevel > ELogLevel::None)
	{
		UE_LOG(Sagittarius, Error, TEXT("%s"), *Msg);
	}
}

that takes an FString as a parameter, as I find this easier to use and more transparent than the UE_LOG macro.

However, when I call this log function from another class:

ASagittarius::LogDebug("Encrypting " + pt + " with key: " + key);

I expected C++ to automatically infer that I am doing concatenation of FStrings (as both pt and key are FStrings). But it does not, throwing these errors at compile time:

error C2784: 'TIndexedContainerIterator operator +(int32,TIndexedContainerIterator)' : could not deduce template argument for 'TIndexedContainerIterator' from 'FString'
error C3767: '+': candidate function(s) not accessible

because "Encrypting " and " with key: " are both const char[] arrays.

It would be great if the + operator worked as expected in such situations, just as the $ operator did in UnrealScript. But in the meantime is there a workaround? This would be helpful not only in this situation but any situation where string concatenation is required, since doing something like:

ASagittarius::LogDebug(FString("Encrypting ") + pt + FString(" with key: ") + key);

seems counterintuitive. Thanks in advance!

Dear William,

I cant say I have the best solution but I have a solution that works great for me and should at least speed things up for you :slight_smile:

from this code:

ASagittarius::LogDebug("Encrypting " + pt + " with key: " + key);

to this code:

//great thing about c++ is you can declare variables anywhere you want!
FString LogText = "Encrypting ";
LogText += pt;
LogText += " with key: ";
LogText += key;
ASagittarius::LogDebug(LogText);

It might look longer and seem slower but actually in practice I find += extremely handy and use it everywhere

There’s probably an even faster way such as you are asking for but hopefully this helps you


FString::Printf()

You can also look up FString::printf (UnrealString.h), it might have what you want, example from source code:

 ASagittarius::LogDebug(FString::Printf(TEXT("Min=(%s) Max=(%s)"), *Min.ToString(), *Max.ToString()));

:slight_smile:

Can’t wait to see your Sagittarius system fully working so I can create server browser for my game!

:slight_smile:

Rama

That Printf seems to be what I’m looking for, where is that example with Min/Max in code? Haha work on Sagittarius is slow going, I’m having to rewrite many things that would be very easy in UScript…but you’ll know when I’m done :slight_smile:

the example is from

IntRect.h 

:slight_smile:

that’s an example of converting FVector’s to their ToString() and then printing out the FString as %s

here’s another example that is for floats, %f, which is the ToString() for FColor’s

//Color.h
 FString ToString() const
 {
  return FString::Printf(TEXT("(R=%f,G=%f,B=%f,A=%f)"),R,G,B,A);
 }

:slight_smile:

Rama

It’s weird, I keep accepting your answer and it never registers as accepted…Anyway, it turns out that passing an FString directly to TEXT doesn’t work, it requires the string pointer instead. But at least it compiles now, thanks!