I have a vector type of which will be determined at runtime. So how can I implement this? I did some work on it. But it is not pretty and optimal. Thank u in advance for any help.
#include <iostream> #include <vector> #include <typeinfo> #include <variant> template <typename T> void fillVector(std::vector<T>& vector) { // fill vector with values } struct A { std::vector<std::variant<int, float, short>> values; }; int main() { A data; int type; std::cin >> type; if (type == 0) // int { std::vector<int> intValues; fillVector(intValues); for (const auto& value : intValues) { data.values.push_back(value); } } else if (type == 1) // short { std::vector<short> shortValues; fillVector(shortValues); for (const auto& value : shortValues) { data.values.push_back(value); } } else // float { std::vector<float> floatValues; fillVector(floatValues); for (const auto& value : floatValues) { data.values.push_back(value); } } return 0; }
void fillVector(std::vector<T1>& vector) { // fill vector with values } void fillVector(std::vector<T2>& vector) { // fill vector with values } void fillVector(std::vector<T3>& vector) { // fill vector with values }
std::vector<std::any> myVecOfAnyValues;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)