Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

FString::Printf problem with fmt arg

In version 4.20 the FString::Printf generates a warning

 FString::Printf((Value > 0) ? TEXT("+%.*f") : TEXT("%.*f"), precision, Value)

warning C4996: 'FString::Printf': The formatting string must now be a TCHAR string literal. Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.

but If I write

 if (Value > 0)
      FString::Printf(TEXT("+%.*f"), precision, Value);
      FString::Printf(TEXT("%.*f"), precision, Value);

No more warning.

Product Version: UE 4.20
more ▼

asked Aug 05 '18 at 07:43 AM in C++ Programming

avatar image

714 49 56 128

avatar image mdavies-opaque Oct 12 '18 at 03:07 AM

This isn't an answer, but I'd love to hear from Epic why they made this change in 4.20. This FString::Printf warning indicates they plan on breaking API compatibility/similarity with sprintf-style formatting, where you may want to pass a TCHAR variable into the formatting string as opposed to a TCHAR string literal, and I can't see an obvious replacement, which seems like a pretty crappy move. I assume it was done for optimisation, but I'd appreciate a workaround as there are plenty of legitimate use cases for wanting to pass in a TCHAR varaible as opposed to a string literal that's known at compile time. For example, if you wanted a function that outputs a formatted float based on a passed-in string format, which may be "%0.1f" or "%.3f" (or many other possibilities), how do we do this from 4.21 onwards the "Epic" way?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

In C++ the first and the second implementation are producing the same result. In 4.19.2 it works. But since Printf changed in 4.20, and it produces a warning and should not work in future release.

What I do not understand, why with a ternary operator it does generates a warning, since the result should be the same.

more ▼

answered Aug 06 '18 at 07:09 AM

avatar image

714 49 56 128

avatar image RVillani Sep 05 '18 at 03:41 PM

I'm wondering the exact same thing.

The implementation that generates a warning is the one where the type comparison in the template fails when looking for an array or ref of TCHAR.

 template <typename FmtType, typename... Types>
     DEPRECATED(4.20, "The formatting string must now be a TCHAR string literal.")
     static typename TEnableIf<!TIsArrayOrRefOfType<FmtType, TCHAR>::Value, FString>::Type Printf(const FmtType& Fmt, Types... Args)

How would a ternary operator change the output enough for that to return false? I just did some research and it seems to only change the output type when two different types are used. Not the case here, though.

avatar image Mauxx91 Dec 12 '18 at 11:40 AM

A ternary operator is evaluated at runtime, so it's not a literal string known at compile time. So it generates the warning.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users


I have not tried that code, but I see a potential problem in your first implementation with the ternary operator. If that evaluates to true the first TEXT macro is not going to receive the arguments and I believe that is what you are seeing. Hope that helps


more ▼

answered Aug 05 '18 at 01:18 PM

avatar image

11 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question