添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I use VS2019.

I have my string class, and I want my wchar_t string will return new char string

String_base<char> to_utf8() const  
	String_base<char> output;  
	std::mbstate_t state = std::mbstate_t();  
	std::size_t len = 1 + std::wcsrtombs(nullptr, (const wchar_t**)&m_data, 0, &state);  
	std::vector<char> mbstr(len);  
	std::wcsrtombs(&mbstr[0], (const wchar_t**)&m_data, mbstr.size(), &state);  // <<<<<<<<< AFTER HERE  
	output.append(&mbstr[0]);  
	return output;  

and after std::wcsrtombs m_data is nullptr

I added this before all actions

const wchar_t* old = m_data;  

and old points to string(everything good), so std::wcsrtombs for some reason made CONST pointer nullptr.

The conversion stops if:

  • The null character was converted and stored. src is set to a null pointer...
  • So yes, setting m_data to null is the expected and documented side effect of the function you use.

    wcsrtombs did not make a const pointer null. In the type const wchar_t**, it's the wchar_t - the pointed-to data - that is const. Not the pointer that points to it (that would have been wchar_t* const *)