Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23798

Invalid literal suffix in MSVC 2017

I have the following code in a file called Console.hpp:

inline std::wstring operator "" _t(const char* s)
{
  std::wstring_convert<codecvt_utf8_utf16<wchar_t>> converter;
  std::wstring wide = converter.from_bytes(s);
  return console::typografix(wide);
}

inline std::wstring operator "" _t(const wchar_t* w, size_t _)
{
  return console::typografix(w);
}

Now, inside Main.cpp, I have an #include "Console.hpp" and am trying to use the suffix

"Like this"_t

Unfortunately I get the following error:

Error C3688 invalid literal suffix '_t'; literal operator or literal operator template 'operator ""_t' not found

Can someone help me figure out what's going on?

Upvotes: 1

Views: 2720

Answers (1)

Ayka
Ayka

Reputation: 51

The reason why error C3688 occurs is that the operator""_t accepting const char * parameter also needs to accept std::size_t like

inline std::wstring operator""_t(const char* s, std::size_t) {
    /* ... */
}

See User-defined literals:

(Before C++20) the user-defined literal expression is treated as a function call operator ""X (str, len), where len is the length of the string literal, excluding the terminating null character.

Since the compiler only finds a valid operator""_t that accepts const wchar_t *, it will not match the string literal "Like this" is for const char * type or std::string type and only match a wstring literal like L"Like this"_t.

Upvotes: 0

Related Questions