I am analyzing columns in one or more large data tables loaded from binary files. Every column can be one of several predefined types and is essentially a vector
. I defined the column to be a variant
of several vectors. In the toy example below, I use only two possible types (in my project, there are 6 or 7 types). Basically, I need a function that loads a single column from a given file. In order to avoid calling the copy constructor of a vector, I'd like to keep every loaded column in the heap memory, that is, have it as a unique_ptr
.
I tried implementing such a function, but the C++ compiler cannot assign the loaded vector pointer to my variant
pointer variable. I am giving an example code snippet below:
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <variant>
#include <vector>
using column_t = std::variant<
std::vector<std::uint32_t>,
std::vector<double>>;
std::unique_ptr<column_t> load(const std::string& file_name) {
column_t* result;
// The column type is encoded in the file contents, but
// in this toy example, we are not reading from the file
switch (file_name.size() % 2) {
case 0:
result = new std::vector<std::uint32_t>();
(std::get<std::vector<std::uint32_t>>(*result)).push_back(5);
break;
default: // 1
result = new std::vector<double>();
(std::get<std::vector<double>>(*result)).push_back(3.14);
}
return std::unique_ptr<column_t>(result);
}
inline std::size_t get_length(const column_t& v) {
switch (v.index()) {
case 0:
return std::get<0>(v).size();
default: // 1
return std::get<1>(v).size();
}
}
int main()
{
std::unique_ptr<column_t> values = load("myfile");
std::cout << "Loaded a vector of length "
<< get_length(*values.get()) << std::endl;
return 0;
}
I know that I can rewrite the function to simply return a column_t
object, however, using a smart pointer would be more intuitive in the downstream analysis tasks. Could anyone point me to the correct construct I need to use in order to assign a pointer (or directly a unique_ptr
instance) to the variable result
?