tags:

views:

74

answers:

2

Hi

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.

+4  A: 

Try:

CreateFont(L"monospace");

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

Jim Lamb
In general, L"string literal" generates a wide-character (`wchar_t`) string literal (which is exactly what you want for `std::wstring`.) Exactly what `wchar_t` is is platform dependent. On Windows a `wchar_t` stores a UTF-16 code point, but on other platforms it could represent, for example, a UTF-32 code point.
jamesdlin
Note that the platform-dependency doesn't matter for the answer. L"" is a `wchar_t[]` and `std::wstring` wants a `wchar_t*`, so this works on all systems, whether `wchar_t` is 8, 16, 32 or 42 bits.
MSalters
+1  A: 
std::wstring s(L"Monospace");
CreateFont(s);

the ctor for wstring doesn't accept narrow characters, only wides...

jspcal