When using CUDA setup in a CMake project (project(test CUDA CXX) directive) the following code throws a SIGABRT:
#include <iostream>
#include <vector>
#include <any>
int main(int argc, char *argv[ ]) {
std::vector<int> vec_test_int = std::vector<int>({5});
std::vector<std::any> vec_test_any = std::vector<std::any> ({5});
int i = 0;
for (auto entry : vec_test_int) {
std::cout << "Entry #" << i << " - Value: " << entry << std::endl;
++i;
}
i = 0;
for (auto entry : vec_test_any) {
std::cout << "Entry #" << i << " - Value: " << std::any_cast<int>(entry) << std::endl;
++i;
}
}
gcc correctly initializes the vector correctly with one int element set to 5, nvcc however initializes the vector with 5 empty any objects.
As per the C++ standard (§13.3.1.7), the initializer_list constructor overload of T (std::any) should take precedence over that of std::vector, as it does in the first case.
Wrapping the int in double braces produces the correct result:
std::vector<std::any> vec_test_any = std::vector<std::any> ({{5}});