<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/126246>126246</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
tuple variant crashing iterating over vector with std::apply (clang 17.0.1 - clang 19.1.0)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
wh1t3lord
</td>
</tr>
</table>
<pre>
## Problem
Here's a function that fills a std::vector<std::variant<Ts...>> from a std::vector<std::string_view>. The function template takes a std::tuple<Ts...> as a type parameter and uses std::apply to iterate over the tuple's types. It attempts to convert each string in the input vector to the corresponding type or keeps it as a string if conversion is not possible.
## Code & Proof
```cpp
#include <iostream>
#include <vector>
#include <variant>
#include <tuple>
#include <string_view>
#include <cstdlib> // for std::atoi, std::atof
#include <type_traits>
template <typename... Ts>
std::vector<std::variant<Ts...>> fill_vector(const std::vector<std::string_view>& input) {
using VariantType = std::variant<Ts...>;
std::vector<VariantType> result;
result.reserve(input.size());
for (const auto& str : input) {
bool converted = false;
// Create a temporary tuple to pass to std::apply
std::tuple<Ts...> tempTuple;
std::apply([&](auto&&... args) {
// Fold expression to try each type
(([&] {
using T = std::decay_t<decltype(args)>;
if (!converted) {
if constexpr (std::is_integral_v<T>) {
try {
result.emplace_back(static_cast<T>(std::atoi(str.data())));
converted = true;
} catch (...) {}
} else if constexpr (std::is_floating_point_v<T>) {
try {
result.emplace_back(static_cast<T>(std::atof(str.data())));
converted = true;
} catch (...) {}
}
}
}()), ...);
}, tempTuple);
if (!converted) {
result.emplace_back(std::string(str)); // Fallback to string
}
}
return result;
}
int main() {
std::vector<std::string_view> input = {"123", "45.67", "hello", "789"};
auto output = fill_vector<int, double, std::string>(input);
for (const auto& v : output) {
std::visit([](const auto& value) { std::cout << value << " "; }, v);
}
return 0;
}
```
Proof №1: https://godbolt.org/z/TPv4vcGch (clang 17.0.1)
Proof №2: https://godbolt.org/z/178zP4x9v (clang 19.1.0)
So both crashes.
### Working sample (Expected behaviour)
Unexpected but it compiles on gcc.
Link: https://godbolt.org/z/W8jqWqzE3 (gcc 12.1)
Link: https://godbolt.org/z/n9GWG4EM8 (gcc 14.2)
## Crash backtrace
Proceed to godbolt site with attached link in previous section.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMV01v2zwS_jX0haggU7YlHXxInaS7wC5QYLNvjgZNjS02tKiSI7XJr38xlCxLduo3vVVIUfNjnvl6ZkhK7_WhAliz5We2vJ_JBkvr1j_KOSbGumK2s8XrmomEiYR_dXZn4MhZfMfiu3-BAyZSzyXfN5VCbSuOpUS-18bQrMeCJXcsuWtBoXUs2ZxnpNOyQpZsnnwURSx5YMkD3zt7vC3n0enqsG01_GDJQ8SfShgph2NtJAJH-QITA7CpDYyUcUnL-FoDr6WTR0BwXFYFbzz4s5isa_PK0XKN4AjYtuA4lsA7QHKeQHzE_41cIhmAngSUrVpwyEGqkndGc10FUV3VDfLONdpKc8o6B762VUEbg13W8ReA2nONnbUnlH0P7sll7XllkdfWe70zEHWZ6dO1sQVwJlaUN7vvl1Zx96fqutupK2Ua2phstPXoQB4pHZdrp1y8s3LK5fVSH_frhWker5aVx8LoHWWKiUcmHvneulFe0GomNpOJ_bXy1xq26KRG3-uI7waK9OuVPEIURfyp3_L7lNXGbPvNIlO28vhx_lJuAh2YyDlLP7P4jnPOG0-J_qvT90RkYMk9v2nISfRa9QiG7HXgG4NngW4cOfDgWmAiC_ZEXr_RgImc_sLuXoASMXgqG7TkhEfHWXL3njP07aw1p5KAInizl8bDBJi-PtkbB5QjGSraOuleu4KjcqmlDxU2LdIRxq-LntCeuskLvRdgIqNuKFZsec9E1jvJxIqYIt3BX3s4Mv7RmoLDz9qBDyVKJe5eu05AlLuSysb63sE9U-JpSoQClHzdEhEKUCZgi6y3b8KK8af3ncr5kI_3vRntD7kmj0hy0K79VlcIByfNtqUwB0LfgqKPQnF7x4iVoVgVbHdS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