Replace preprocessor macro of string literal with concatenation [duplicate] - c++

This question already has answers here:
Simplest way to combine two strings at compile time with C++11
(2 answers)
How to concatenate static strings at compile time?
(2 answers)
Concatenate compile-time strings in a template at compile time?
(4 answers)
Closed 8 months ago.
I am attempting to replace as many uses of macros in our code with proper c++17 constructs. How would I replace the following macro with a constexpr or something else?
#define FNAME "first"
#define LNAME "last"
#define NAME FNAME LNAME
const char hello[] = "hello " NAME;

Related

sprintf cause programm stopping [duplicate]

This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 months ago.
std::string sszModName = "kernel32.dll";
std::string WinVersion = "WIN81";
std::string MachineGUID= "ce9e95db-5fda-436a-b29a-f5537702c77d";
char buf[1024];
sprintf(buf, "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", sszModName, "ERROR_HASH_VERIFY", WinVersion, MachineGUID);
This code causes program lag, could you help me figure out why?
Try
sprintf(buf,
"https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s",
sszModName.c_str(),
"ERROR_HASH_VERIFY",
WinVersion.c_str(),
MachineGUID.c_str());
C strings are not the same as C++ strings. spprintf only uses C strings so you must use .c_str() to turn your C++ strings into C strings.

C++ raw string with special char [duplicate]

This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";

Hash symbol after define macro? [duplicate]

This question already has answers here:
# and ## in macros
(3 answers)
Stringification - how does it work?
(2 answers)
Closed 5 years ago.
What does the '#' symbol do after the second define? And isn't the second line enough? Why the first one?
#define MAKESTRING(n) STRING(n)
#define STRING(n) #n
This is stringize operation, it will produce a string literal from macro parameter, e.g. "n". Two lines are required to allow extra expantion of macro parameter, for example:
// prints __LINE__ (not expanded)
std::cout << STRING(__LINE__) << std::endl;
// prints 42 (line number)
std::cout << MAKESTRING(__LINE__) << std::endl;
Hash symbol takes macro argument into a c-string.
For example
#define MAKESTRING(x) #x
printf(MAKESTRING(text));
will print text
And first line is only alternative name for this macro.

Add the " symbol in a QStringList [duplicate]

This question already has answers here:
Rules for C++ string literals escape character
(6 answers)
Closed 6 years ago.
I am trying to create a QStringList containing all punctuation signs.
How can I add the element " into it ?
You can use \ to escape the character ". The code may look like this:
QStringList foo;
foo << "\"";
An other option would be to construct a QString from a char declared between simple quotes ':
foo << QString('"');
Since the constructor isn't declared as explicit in documentation, this should also work with implicit conversion:
foo << '"';

Some questions with printing output in C++ [duplicate]

This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).