This has nothing to do with Qt. Std::tostring is C11, you have to make sure you have a recent enough compiler and to enable C11 features. If you use Qt Creator you should add CONFIG += c11 in your.pro file and re-run qmake. Dec 28, 2017 I notice that std::tostring support is missing from the libstdc, not sure what it takes to get that added, but it is useful especially when porting other C11 code. Specifically, the following. Unlike other formatting functions in C and C libraries, std::tochars is locale-independent, non-allocating, and non-throwing. Only a small subset of formatting policies used by other libraries (such as std::sprintf) is provided. This is intended to allow the fastest possible implementation that is useful in common high-throughput contexts such as text-based interchange (JSON or XML).
-->With C++/WinRT, you can call Windows Runtime APIs using C++ Standard Library wide string types such as std::wstring (note: not with narrow string types such as std::string). C++/WinRT does have a custom string type called winrt::hstring (defined in the C++/WinRT base library, which is %WindowsSdkDir%Include<WindowsTargetPlatformVersion>cppwinrtwinrtbase.h
). And that's the string type that Windows Runtime constructors, functions, and properties actually take and return. But in many cases—thanks to hstring's conversion constructors and conversion operators—you can choose whether or not to be aware of hstring in your client code. If you're authoring APIs, then you're more likely to need to know about hstring.
There are many string types in C++. Variants exist in many libraries in addition to std::basic_string from the C++ Standard Library. C++17 has string conversion utilities, and std::basic_string_view, to bridge the gaps between all of the string types. winrt::hstring provides convertibility with std::wstring_view to provide the interoperability that std::basic_string_view was designed for.
Using std::wstring (and optionally winrt::hstring) with Uri
Windows::Foundation::Uri is constructed from a winrt::hstring.
But hstring has conversion constructors that let you work with it without needing to be aware of it. Here's a code example showing how to make a Uri from a wide string literal, from a wide string view, and from a std::wstring.
The property accessor Uri::Domain is of type hstring.
But, again, being aware of that detail is optional thanks to hstring's conversion operator to std::wstring_view.
To_string Is Not A Member Of Std C++
Similarly, IStringable::ToString returns hstring.
Uri implements the IStringable interface.
You can use the hstring::c_str function to get a standard wide string from an hstring (just as you can from a std::wstring).
If you have an hstring then you can make a Uri from it.
Consider a method that takes an hstring.
All of the options you've just seen also apply in such cases.
hstring has a member std::wstring_view conversion operator, and the conversion is achieved at no cost.
winrt::hstring functions and operators
A host of constructors, operators, functions, and iterators are implemented for winrt::hstring.
An hstring is a range, so you can use it with range-based for
, or with std::for_each
. It also provides comparison operators for naturally and efficiently comparing against its counterparts in the C++ Standard Library. And it includes everything you need to use hstring as a key for associative containers.
We recognize that many C++ libraries use std::string, and work exclusively with UTF-8 text. As a convenience, we provide helpers, such as winrt::to_string and winrt::to_hstring, for converting back and forth.
WINRT_ASSERT
is a macro definition, and it expands to _ASSERTE.
For more examples and info about hstring functions and operators, see the winrt::hstring API reference topic.
The rationale for winrt::hstring and winrt::param::hstring
The Windows Runtime is implemented in terms of wchar_t characters, but the Windows Runtime's Application Binary Interface (ABI) is not a subset of what either std::wstring or std::wstring_view provide. Using those would lead to significant inefficiency. Instead, C++/WinRT provides winrt::hstring, which represents an immutable string consistent with the underlying HSTRING, and implemented behind an interface similar to that of std::wstring.
You may notice that C++/WinRT input parameters that should logically accept winrt::hstring actually expect winrt::param::hstring. The param namespace contains a set of types used exclusively to optimize input parameters to naturally bind to C++ Standard Library types and avoid copies and other inefficiencies. You shouldn't use these types directly. If you want to use an optimization for your own functions then use std::wstring_view. Also see Passing parameters into the ABI boundary.
The upshot is that you can largely ignore the specifics of Windows Runtime string management, and just work with efficiency with what you know. And that's important, given how heavily strings are used in the Windows Runtime.
Formatting strings
One option for string-formatting is std::wostringstream. Here's an example that formats and displays a simple debug trace message.
The correct way to set a property
You set a property by passing a value to a setter function. Here's an example.
The code below is incorrect. It compiles, but all it does is to modify the temporary winrt::hstring returned by the Text() accessor function, and then to throw the result away.
Important APIs
C++ Std String To Int
|