constexpr version of FString

Hey all,

Quick question regarding the string containers in Unreal’s C++ - can you construct a valid constexpr for FString (or TStringView<>, or FName etc.). As far as I can tell this is possible in the modern C++ standard library (godbolt link) but when trying something similar for FString, the compiler spits this output out:

constexpr FString G_AUTOSAVE_SLOT_NAME = "Autosave";

// Example usage:
UGameplayStatics::AsyncSaveGameToSlot(SaveGameData, G_AUTOSAVE_SLOT_NAME, G_AUTOSAVE_USER_INDEX, SaveGameDelegate);

  [omitted]\JJEDefinitions.h(5): error C2131: expression did not evaluate to a constant
  [omitted]\JJEDefinitions.h(5): note: failure was caused by call of undefined function or one not declared 'constexpr'
  [omitted]\JJEDefinitions.h(5): note: see usage of 'FString::FString'
  [omitted]\JJEDefinitions.h(5): note: the call stack of the evaluation (the oldest call first) is
  [omitted]\JJEDefinitions.h(5): note: while evaluating function 'FString::FString(const ANSICHAR *)'
1 Like

Hi, I was testing the same thing and it looks like unfortunately we can’t have constexpr FString because FString is not a literal type.

Other working alternatives:

//Works as global FString for example for function inputs, but is not evaluated at compile time as constexpr
const FString G_AUTOSAVE_SLOT_NAME = "Autosave"; 

//Evaluated at compile time, but in some cases where TCHAR* is not accepted you will need to wrap it with FString, like FString(G_AUTOSAVE_SLOT_NAME)
constexpr const TCHAR* G_AUTOSAVE_SLOT_NAME  = TEXT("Autosave");