operator+ ambiguity with std::wstring on Windows

7 views
Skip to first unread message

Vaishnav Katiyar

unread,
Dec 16, 2025, 3:54:08 AM12/16/25
to icu-support

Hello ICU Team,

 

The new templated operator+ overload introduced in ICU 76.1 causes compilation errors on Windows when concatenating std::wstring with wide character arrays.

 

Issue:

The templated operator+ in unicode/unistr.h:

 

template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>

inline UnicodeString operator+(const UnicodeString &s1, const S &s2)

 

creates ambiguity with std::operator+ because on Windows (where sizeof(wchar_t) == 2), the template matches wchar_t[N] arrays through the ConvertibleToU16StringView trait.

 

Minimal Reproducer:

#include <string>

#include <unicode/unistr.h>

 

int main() {

    std::wstring path = L"C:\\temp";

    wchar_t filename[260] = L"test.txt";

   

    // SCENARIO 1: WORKS - Using operator+=

    std::wstring result1 = path + L"\\";

    result1 += filename;

   

    // SCENARIO 2: FAILS - Using operator+

    std::wstring result2 = path + L"\\" + filename;

   

    return 0;

}

 

Compilation Error (MSVC):

error C2666: 'operator+': overloaded functions have similar conversions

- std::operator+(std::wstring, const wchar_t*)

- icu::operator+(const UnicodeString&, const wchar_t(&)[260])

 

Impact:

This breaks existing code, including Boost.Interprocess (boost/interprocess/detail/os_file_functions.hpp:380) where similar string concatenation patterns are used:

strFilePath = refcstrRootDirectory + backslash + FileInformation.cFileName;

 

This overload has been moved out of the DRAFT API in the latest ICU release. Could you please advise on the recommended way to proceed for affected projects?

Should we expect a further update or workaround, or is there a migration path you would suggest?

 

Thank you for your consideration and guidance.

 

Best regards,

Vaishnav

 

Markus Scherer

unread,
Dec 16, 2025, 2:07:38 PM12/16/25
to Vaishnav Katiyar, icu-support
On Tue, Dec 16, 2025 at 12:54 AM 'Vaishnav Katiyar' via icu-support <icu-s...@unicode.org> wrote:

Compilation Error (MSVC):

error C2666: 'operator+': overloaded functions have similar conversions

- std::operator+(std::wstring, const wchar_t*)

- icu::operator+(const UnicodeString&, const wchar_t(&)[260])

 

Impact:

This breaks existing code, including Boost.Interprocess (boost/interprocess/detail/os_file_functions.hpp:380) where similar string concatenation patterns are used:

strFilePath = refcstrRootDirectory + backslash + FileInformation.cFileName;


So this improvement for ICU has the unintended consequence of breaking existing code that includes ICU header files but does not actually use icu::UnicodeString.
Oops :-(

Could you please create a Jira ticket?

I briefly discussed this with a colleague. It should be possible to fix this by adding yet more overloads in ICU, with explicit pointer types on the right side of operator+(). We would need three or four additional overloads for different pointer types. We would also need to be careful not to run into yet more unintended consequences.

markus

Robin Leroy

unread,
Dec 17, 2025, 7:46:49 PM12/17/25
to Markus Scherer, Vaishnav Katiyar, icu-support
Le mar. 16 déc. 2025 à 20:07, Markus Scherer <marku...@gmail.com> a écrit :
Could you please create a Jira ticket?
 
I don’t think Vaishnav got to that yet, so I filed https://unicode-org.atlassian.net/browse/ICU-23299.

While looking at that, it occurred to me that we should probably have more of these operators—though we should be careful not to introduce ambiguities in code unrelated to ICU this time—; filed https://unicode-org.atlassian.net/browse/ICU-23298 to look into that.

Best regards,

Robin Leroy

Markus Scherer

unread,
Dec 18, 2025, 1:59:35 PM (14 days ago) 12/18/25
to Robin Leroy, Vaishnav Katiyar, icu-support
On Wed, Dec 17, 2025 at 4:46 PM Robin Leroy <eggr...@unicode.org> wrote:
Le mar. 16 déc. 2025 à 20:07, Markus Scherer <marku...@gmail.com> a écrit :
Could you please create a Jira ticket?
 
I don’t think Vaishnav got to that yet, so I filed https://unicode-org.atlassian.net/browse/ICU-23299.

We agreed to add this to a future ICU 78.2 maintenance release, and Robin has the same fix ready for the maint/maint-78 branch as well:

Best regards,
markus
Reply all
Reply to author
Forward
0 new messages