I have a function which takes const std::wstring&
font_family, i.e.
Font Font::CreateFont(const std::wstring& font_family){ ... }
By question is how can I call that funcion by passing a string literal (e.g monospace)?
I tried
CreateFont("monospace");
CreateFont("std::wstring("monospace") );
Both does not compile. Any one have better idea?
Thank you.
If you want to pass the string literal "directly" (without constructing an std::string), the most common way is to pass the pointer to it. This code is also legal: const int& ten = 10; Is the compiler simply replacing the reference with the literal wherever it appears?
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
std::string literals are Standard Library implementations of user-defined literals (see below) that are represented as "xyz"s (with a s suffix). This kind of string literal produces a temporary object of type std::string , std::wstring , std::u32string , or std::u16string , depending on the prefix that is specified.
String constants, also known as string literals, are a special type of constants which store fixed sequences of characters. A string literal is a sequence of any number of characters surrounded by double quotes: "This is a string."
Try:
CreateFont(L"monospace");
The leading "L" directs the compiler to generate a wide (wchar_t) string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With