You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
question: how to correctly create print helpers for '<<' explicitly removed in C++20? #409
Hello!
while project description states that this framework is for C++17 max, could anyone possibly help me with C++20 compatibility issue?
my problem is that in C++20 operator << for printing wide strings (std::wstring, `const wchar_t*, etc) was explicitly removed. In C++17 I could define helper template like
template<> std::ostream& operator<<(std::ostream& o, const std::wstring& what) { o << convert_to_char(what); return o; }
and it worked perfectly. In C++20 this no longer works, as this operator is explicitly deleted, so this template is not even considered. I found that I can partially solve the issue for std::wstring by defining this specialization
It turned out that I was incorrect in my analysis of the problem: it turned out that old workaround with std::ostream& operator<<(std::ostream& o, const std::wstring& what) works just fine. But with C++20 this workaround no longer works for wide-string literals like L"expectation": when C++ tries to apply td::ostream& operator<<(std::ostream& o, const wchar_t* what) it finds that it is deleted, so conversion to std::wstring is not considered. So obvious fix is to use wstring's, like
using std::string_literals;
BOOST_TEST(someWideString == L"expectations"s);
but is there a way to make BOOST_TEST to work with const wchar_t* ?
using std::string_literals;
BOOST_TEST(someWideString == L"expectations"s);
but is there a way to make BOOST_TEST to work with const wchar_t* ?
BOOST_TEST does work with const wchar_t* (not necessarily printing the string); it just doesn't work with const wchar_t[N]. BOOST_TEST has special handling for pointers to arrays of characters because pointers might be null. Arrays, on the other hand, rely on the presence of <<.
Activity
mbel1 commentedon Jan 30, 2024
It turned out that I was incorrect in my analysis of the problem: it turned out that old workaround with
std::ostream& operator<<(std::ostream& o, const std::wstring& what)works just fine. But with C++20 this workaround no longer works for wide-string literals likeL"expectation": when C++ tries to applytd::ostream& operator<<(std::ostream& o, const wchar_t* what)it finds that it is deleted, so conversion tostd::wstringis not considered. So obvious fix is to use wstring's, likebut is there a way to make
BOOST_TESTto work withconst wchar_t*?zhihaoy commentedon Apr 18, 2024
BOOST_TESTdoes work withconst wchar_t*(not necessarily printing the string); it just doesn't work withconst wchar_t[N].BOOST_TESThas special handling for pointers to arrays of characters because pointers might be null. Arrays, on the other hand, rely on the presence of<<.My current workaround is