I'm looking to try to write some generic code that can handle different data types. Once these data types are set, then it'll remain the same throughout the duration of the instance.
I think it'll be easier to show what I'm trying to do, rather than describe it.
helper.h
#include <iostream>
#include <type_traits>
#include <utility>
#include <string>
#include <sstream>
#include <stdexcept>
using namespace std;
template <typename T>
class helper
{
public:
helper()
{
stringstream temp;
if(is_same<T, short>::value)
{
temp << 1;
}
else if(is_same<T,long>::value)
{
temp << 1024;
}
else if(is_same<T, char*>::value)
{
temp << "Hello";
}
else if(is_same<T, string>::value)
{
temp << "Hello World";
}
else
{
throw invalid_argument("Error in helper: Unknown data type" + to_string(__LINE__) + string(__FILE__));
}
temp >> data;
}
T getData()
{
return data;
}
protected:
T data;
};
call.cpp
#include <iostream>
#include "helper.h"
using namespace std;
int main()
{
helper<> my_helper;
int data;
cin >> data;
switch(data)
{
case 1:
my_helper = helper<short>;
break;
case 2:
my_helper = helper<long>;
break;
case 3:
my_helper = helper<char *>;
break;
default:
my_helper = helper<string>;
break;
}
cout << my_helper.getData() << endl;
return 0;
}
Now, this won't compile because helper doesn't have a template argument, but is there a way that I can set the argument at a later point in time (like after there is user input as shown in the example)? Once the argument is set, there is not use case where it'll change. I know this is a trivial example where I could just do a cout
in the switch-case
, but this is the concept that I want to accomplish.
Unfortunately I'm stuck with C++11 and no boost libs, otherwise I think I could use std::any
, I thought I could use void
pointers, but then I'd have to specify what the datatype is when I call a reinterpret_cast
.
If there is any additional information that I can provide or anything I can clear up, please let me know!