Reputation: 23629
I want to define a string_view
literal using the sv
operator, but since my application can be compiled as either ANSI or UNICODE I use Microsoft's _T
macro from tchar.h
.
Since I want to apply the sv
operator on a wchar_t
pointer/string I assumed I had to write this:
using namespace std::literals;
constexpr static auto H1{_T("hello")sv};
But this does not compile and gives the following errors:
error C2146: syntax error: missing ')' before identifier 'sv'
error C2143: syntax error: missing ';' before '}'
error C2059: syntax error: '}'
However, when I write this, it compiles and works correctly:
using namespace std::literals;
constexpr static auto H2{_T("hello"sv)};
The first alternative seems more logical to me since the second alternative looks like the sv
operator is applied on a char
-pointer/string, while actually it is being applied on a Unicode string (if I compile in Unicode using the /D_UNICODE
option).
Strange thing is that if I try to compile the following code:
using namespace std::literals;
constexpr static auto H1{_T("hello")sv};
constexpr static auto H2{_T("hello"sv)};
constexpr static auto H3{L"hello"sv};
then the first line does not compile, as explained above. But when I add the /EP
option to the compiler (so I get a file with the output of the preprocessor), I see the preprocessor translates these lines to this:
using namespace std::literals;
constexpr static auto H1{L"hello"sv};
constexpr static auto H2{L"hello"sv};
constexpr static auto H3{L"hello"sv};
And compiling this output succeeds.
So preprocessing-only and then compiling works. Compiling in one go, doesn't.
Is there anything in the C++ standard that forbids me to use the first alternative (which seems more logical to me)? If not, is this a bug in Microsofts compiler?
Upvotes: 0
Views: 222