Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string literal to a function which takes const std::wstring&

Tags:

c++

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.

like image 941
n179911 Avatar asked Jan 19 '10 06:01

n179911


People also ask

How do you pass a string literal in C++?

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?

What is std :: string& in C++?

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.

Is std::string literal?

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.

Is string literal Const?

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."


1 Answers

Try:

CreateFont(L"monospace");

The leading "L" directs the compiler to generate a wide (wchar_t) string.

like image 146
Jim Lamb Avatar answered Oct 05 '22 22:10

Jim Lamb