How to convert std::string to LPCSTR?

Call c_str() to get a const char * (LPCSTR) from a std::string. It’s all in the name: LPSTR – (long) pointer to string – char * LPCSTR – (long) pointer to constant string – const char * LPWSTR – (long) pointer to Unicode (wide) string – wchar_t * LPCWSTR – (long) pointer to constant Unicode … Read more

How do you properly use WideCharToMultiByte

Here’s a couple of functions (based on Brian Bondy’s example) that use WideCharToMultiByte and MultiByteToWideChar to convert between std::wstring and std::string using utf8 to not lose any data. // Convert a wide Unicode string to an UTF8 string std::string utf8_encode(const std::wstring &wstr) { if( wstr.empty() ) return std::string(); int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), … Read more

How to clear stringstream? [duplicate]

Typically to ‘reset’ a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear. parser.str( std::string() ); parser.clear(); Typically what happens is that the first >> reaches the end of the string and sets the eof bit, although it successfully … Read more

Why should exceptions be used conservatively?

The primary point of friction is semantics. Many developers abuse exceptions and throw them at every opportunity. The idea is to use exception for somewhat exceptional situation. For example, wrong user input does not count as an exception because you expect this to happen and ready for that. But if you tried to create a … Read more

Pretty-print std::tuple

Yay, indices~ namespace aux{ template<std::size_t…> struct seq{}; template<std::size_t N, std::size_t… Is> struct gen_seq : gen_seq<N-1, N-1, Is…>{}; template<std::size_t… Is> struct gen_seq<0, Is…> : seq<Is…>{}; template<class Ch, class Tr, class Tuple, std::size_t… Is> void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is…>){ using swallow = int[]; (void)swallow{0, (void(os << (Is == 0? “” : “, “) << std::get<Is>(t)), … Read more