From the reference it is known that QStringLiteral places the text directly in the executable file in the read-only area. However, I did not find the information that in case of using the same lines in different places of the project code, these lines will be stored according to the uniqueness of the value. Or will each instance of the string be saved separately?

Suppose there is a network request to some external resource. This query has a string command, say, " https://ru.stackoverflow.com/ ". There are also two classes: class A and class B In both, the specified query is required:

 class A { void getSo() { sendRequest(QStringLiteral("https://ru.stackoverflow.com/")); // Далее выполняем что-то специфическое для класса A. } }; class B { void getSo() { sendRequest(QStringLiteral("https://ru.stackoverflow.com/")); // Далее выполняем что-то специфическое для класса B. } }; 

The question is what exactly gets into the executable file: two lines with the address of the request or one? Or do I misunderstand the principle of the specified macro?

1 answer 1

Standard not specified. Depends on the compiler and settings. For example, in Visual C ++ 2015 there is an option (depending on the optimization parameters used)

/ Gf turn on read-only string concatenation

So these literals will be merged. But if you compile without this key or with /GF- , then in exe'shnike each will take its place ( with disabled optimization - the inclusion of optimization automatically enables this option).

  • Give the source where this key is described. - Cerbo
  • cl /? and look at the result ... Plus it turns on / off, apparently, depending on the optimization settings. - Harry
  • Checked, GF- no, looked at vc2015 - Cerbo
  • one
  • one
    So this is undocumented. Here is the code #include <iostream> int main() { char * s = "stack overflow"; char * v = "stack overflow"; std::cout << s << v; } #include <iostream> int main() { char * s = "stack overflow"; char * v = "stack overflow"; std::cout << s << v; } #include <iostream> int main() { char * s = "stack overflow"; char * v = "stack overflow"; std::cout << s << v; } Next - cl /GF- /EHsc test.cpp works cl /GF- /EHsc test.cpp , in exe there are two lines. cl /GF /EHsc test.cpp - also works. In exe she is alone. Everything. Why the /GF- works and why it is not described - I do not know. Next to her in cl /? a lot of options /G?[-] , which work with a minus. Well, I tried by analogy ... - Harry