UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StringOverload.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10#include "Traits/IsTString.h"
11#include <type_traits>
12
13// A type which wraps a TString parameter but fails (SFINAE) construction if passed a string argument of an incompatible character type.
14// This will allow FString (TString<WIDECHAR>) and FUtf8String (TString<UTF8CHAR>) overloads to sit next to each other and direct
15// pointer, array or other TString arguments to the right overload.
16//
17// This is important because of the legacy of UE's string constructors, which takes anything that looks like a string, of any character
18// width, so overloading - say - FString and FUtf8String will cause overload resolution problems.
19//
20// The argument should be copied/moved into the real TString instance in order to use it, as this type has no string manipulation
21// ability.
22//
23// This argument is equivalent to a pass-by-value toboth the lvalue/rvalue overload case. Once , and can be converted back to regular
24// const TString&/TString&& overloads once overloading is no longer required.
25//
26// Example:
27// void Func(TStringOverload<FWideString>); // called by Func(WideString), Func(MoveTemp(WideString)) or Func(TEXT("String"))
28// void Func(TStringOverload<FUtf8String>); // called by Func(Utf8String), Func(MoveTemp(Utf8String)), Func(UTF8TEXT("String")) or Func("String") or Func({})
29template <typename StringType>
31{
32private:
33 static_assert(std::is_same_v<StringType, std::remove_cv_t<StringType>> && TIsTString_V<StringType>, "TStringOverload expects an unqualified string object type");
34
35 using CharType = typename StringType::ElementType;
36
37 template <typename ArgType>
38 static constexpr bool IsValidArgType()
39 {
40 using DecayedArgType = std::decay_t<ArgType>;
41
43 {
44 return false;
45 }
47 {
49 }
50 else if constexpr (std::is_pointer_v<DecayedArgType>)
51 {
53 }
54 else
55 {
56 static_assert(sizeof(ArgType) == 0, "Unsupported argument type passed to TStringOverload");
57 return false;
58 }
59 }
60
61public:
62 // This Construct allows us to default construct a TStringOverload using {}.
63 // It only exists for a narrow string, because {} needs to resolve one way or another, and we want it
64 // to be FUtf8String by default.
65 template <
66 typename T = CharType
67 UE_REQUIRES(sizeof(T) == 1)
68 >
70 {
71 }
72
73 template <
74 typename ArgType
76 >
78 : String((ArgType&&)Arg)
79 {
80 }
81
82 StringType&& MoveTemp()
83 {
84 // Shouldn't need a const overload, because these types are only intended as by-non-const-value function parameters, and the only
85 // thing you can do with it is move it into a real string object.
86 return (StringType&&)String;
87 }
88
89 StringType String;
90};
91
92namespace UE::Core::Private
93{
94 template <typename CharType, typename ArgType>
96 {
97 using DecayedArgType = std::decay_t<ArgType>;
98
100 {
101 static_assert(std::is_same_v<const TElementType_T<DecayedArgType>, const CharType>, "Incorrect string type passed to UE_PRIVATE_TO_STRING macro");
102 }
103 else if constexpr (std::is_pointer_v<DecayedArgType>)
104 {
105 static_assert(std::is_same_v<const std::remove_pointer_t<DecayedArgType>, const CharType>, "Incorrect string type passed to UE_PRIVATE_TO_STRING macro");
106 }
107 else
108 {
109 static_assert(sizeof(ArgType) == 0, "Unsupported argument type passed to TStringOverload");
110 }
111 return (ArgType&&)Arg;
112 }
113}
114
115// NOT FOR GENERAL USE!
116//
117// These macros exist to make it easier to denote where wide<->narrow conversions are needed due to legacy APIs,
118// so they can be located and fixed later.
119#define UE_PRIVATE_TO_UTF8_STRING(Str) FUtf8String(UE::Core::Private::CheckCharType<WIDECHAR>(Str))
120#define UE_PRIVATE_TO_WIDE_STRING(Str) FWideString(UE::Core::Private::CheckCharType<UTF8CHAR>(Str))
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_REQUIRES(...)
Definition Requires.h:86
implementation
Definition PlayInEditorLoadingScope.h:8
ArgType && CheckCharType(ArgType &&Arg)
Definition StringOverload.h:95
Definition IsContiguousContainer.h:16
Definition StringOverload.h:31
StringType String
Definition StringOverload.h:89
TStringOverload(ArgType &&Arg)
Definition StringOverload.h:77
TStringOverload()
Definition StringOverload.h:69
StringType && MoveTemp()
Definition StringOverload.h:82