Union based encoding of .csv file into a data table with different types I am writing a program that has a row-based data table class. It also has an encoder class that is intended to take in some .csv data and convert it into a 2darray of encoded data of unknown type at compile time. 

The idea is that the user will point to a .csv file they would like to read from and specify a runtime schema (column 1 == int, column 2 == bool, column 3 == string, etc) that will be used to encode the raw .csv data into the proper types needed for comparison and such elsewhere in the program. 

I have been reading a lot about this and decided to try a union based approach. 
I mocked this up in a simple project to see how this might work out on one single piece of data ("DataPoint."). Here is the code. 


#include "pch.h"
#include &lt;iostream&gt;
#include &lt;sstream&gt;


union DataPoint
{
    bool Bool;
    int  Integer;
    float FloatingPoint;
    //std::string String;
};

enum DataTypes
{
    Bool,
    Integer,
    FloatingPoint,
    //String
};


DataPoint EncodeData(DataPoint InDataPoint, DataTypes InDataTypes, std::string InData)
{
    std::stringstream DataStream(InData);

    switch (InDataTypes)
    {
    case Bool: 
        std::cout &lt;&lt; "Type is Bool" &lt;&lt; std::endl;
        if (InData == "True" || InData == "true"  || InData == "1") InDataPoint.Bool = true;
        else if (InData == "False" || InData == "false" || InData == "0") InDataPoint.Bool = false;
        else std::cout &lt;&lt; "This data is not compatable with the bool type." &lt;&lt; std::endl;
        break;
    case Integer: 
        std::cout &lt;&lt; "Type is Integer" &lt;&lt; std::endl;
        DataStream &gt;&gt; InDataPoint.Integer;
        break;
    case FloatingPoint: 
        std::cout &lt;&lt; "Type is Floating Point" &lt;&lt; std::endl;
        DataStream &gt;&gt; InDataPoint.FloatingPoint;
        break;
    /*
    case String: InDataPoint.String = InData;
        break;
    */
    default:
        break;
    }

    return InDataPoint;
}

void PrintData(DataPoint InDataPoint, DataTypes InDataTypes)
{
    switch (InDataTypes)
    {
    case Bool: std::cout &lt;&lt; "Encoded data value : " &lt;&lt; InDataPoint.Bool &lt;&lt; std::endl;
        break;
    case Integer: std::cout &lt;&lt; "Encoded data value : " &lt;&lt; InDataPoint.Integer &lt;&lt; std::endl;
        break;
    case FloatingPoint: std::cout &lt;&lt; "Encoded data value : " &lt;&lt; InDataPoint.FloatingPoint&lt;&lt; std::endl;
        break;
    /*
    case String:
        break;
    */
    default:
        break;
    }
}

int main()
{
    DataPoint DataPoint1{};
    DataTypes DataType{ Bool };
    std::string Data{ "true" };

    std::cout &lt;&lt; "Raw data value : " &lt;&lt; Data &lt;&lt; std::endl;

    PrintData(EncodeData(DataPoint1, DataType, Data), DataType);

    return 0;
}



This all seems to work, however, as you can see... I have commented out string entries because I cannot figure out a good way to handle them. One issue I run into is though unions apparently support strings in c++ now, I error out when I try to add a string as a union member. Here is the error I get if I uncomment the string member of the union. 

Severity   Code    Description Project File    Line    Suppression State
Error (active)  E1776   function "DataPoint::DataPoint(const DataPoint &amp;)" (declared implicitly) cannot be referenced -- it is a deleted function  

Is there some way to handle the string?
Also, in general, does this approach seem sound?
I don't have time to check all now but I found some things I want to share

If you are using C++17 consider using std::variant instead of the weak union inherited from c. It will probaly also solve youre isue with std::string. See: https://en.cppreference.com/w/cpp/utility/variant

Consider using '\n' instead of std::endl. Im pretty sure you only want a newline and not also a expensive flush of the buffer: https://stackoverflow.com/questions/213907/c-stdendl-vs-n.

Consider using the more safe enum class instead of plain c enum. It is available since C++11.