Skip to content

Commit 17adb0c

Browse files
committed
fix(ct2-build): patch MSVC template-arg-deduction crash in decoding.cc:1140
Building CTranslate2 v4.4.0 with MSVC (VS 2022 17.10 / VS 2026 Insiders 18.0) in C++20 mode crashes inside decoding.cc when decode_alternatives is instantiated. The failing call is: StorageView input_ids({1, prefix_length}, std::vector<int32_t>(start_tokens.begin(), start_tokens.begin() + prefix_length), device); MSVC's template-arg deduction for the StorageView(Shape, const std::vector<T>&, Device) ctor fails to bind T=int32_t and instead tries std::vector<int32_t>::vector<size_t*, size_t*> (treating the vector as constructed from two iterators), then explodes trying to deduce the 'value_type' for _Uninitialized_backout_al. GCC and clang are unaffected — purely MSVC's C++20 deduction. Workaround: rewrite the call as a 2-step construction. Materialise the std::vector in a local first, then pass it to StorageView. The local has a fully-determined type so MSVC's deduction has no ambiguity to chase: auto _ct2_prefix_ids = std::vector<int32_t>(start_tokens.begin(), start_tokens.begin() + prefix_length); StorageView input_ids({1, prefix_length}, _ct2_prefix_ids, device); Patch is applied post-FetchContent_MakeAvailable (when ctranslate2-src is on disk but before its add_subdirectory compiles) via a string(REPLACE) on decoding.cc, gated on MSVC. GCC and clang see no change. Test: \.\ct2-build.cmd\ should now complete on the dev's VS 2026 Insiders host. GCC and clang paths untouched. Ref: same pattern as the upstream issue tracker would surface (probably a C++20 modules/concepts regression in MSVC).
1 parent 887a734 commit 17adb0c

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎electron/native/ctranslate2-server/CMakeLists.txt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ set(WITH_TENSOR_PARALLEL OFF CACHE BOOL "" FORCE)
116116

117117
FetchContent_MakeAvailable(ctranslate2 httplib json)
118118

119+
# ponytail: MSVC C++20 has a template-arg-deduction bug with the pattern
120+
# StorageView({1, prefix_length}, std::vector<int32_t>(iter, iter), device)
121+
# (decoding.cc:1140). The compiler tries to instantiate
122+
# std::vector<int32_t>::vector<size_t*, size_t*>
123+
# and explodes, even though the type T of the vector is fully determined
124+
# from the explicit `<int32_t>`. Empirically fails on MSVC 19.40+ (VS
125+
# 2022 17.10 / VS 2026 Insiders 18.0). GCC and clang are fine.
126+
#
127+
# Workaround: rewrite the call to first materialize the std::vector in
128+
# a local — that gives MSVC a fully-typed rvalue to match against the
129+
# template ctor `StorageView(Shape, const std::vector<T>&, Device)`
130+
# without going through the (iter, iter) path. Patch is applied
131+
# post-FetchContent (after ctranslate2-src/decoding.cc is on disk but
132+
# before CTranslate2's add_subdirectory compiles it).
133+
if(MSVC)
134+
set(_ct2_decoding_src "${ctranslate2_SOURCE_DIR}/src/decoding.cc")
135+
if(EXISTS "${_ct2_decoding_src}")
136+
file(READ "${_ct2_decoding_src}" _ct2_decoding_content)
137+
string(REPLACE
138+
" StorageView input_ids({1, prefix_length},\n std::vector<int32_t>(start_tokens.begin(),\n start_tokens.begin() + prefix_length),\n device);"
139+
" auto _ct2_prefix_ids = std::vector<int32_t>(start_tokens.begin(), start_tokens.begin() + prefix_length);\n StorageView input_ids({1, prefix_length}, _ct2_prefix_ids, device);"
140+
_ct2_decoding_content_patched "${_ct2_decoding_content}")
141+
if(NOT _ct2_decoding_content STREQUAL _ct2_decoding_content_patched)
142+
file(WRITE "${_ct2_decoding_src}" "${_ct2_decoding_content_patched}")
143+
message(STATUS "Patched CTranslate2 decoding.cc for MSVC vector ctor bug (storage_view.h:1140)")
144+
endif()
145+
endif()
146+
endif()
147+
119148
add_executable(ctranslate2-server
120149
src/main.cpp
121150
src/mel.cpp

0 commit comments

Comments
 (0)