Convert C++ template parameter to string in a structure [duplicate]

  c++, templates

I need to convert a template value to a string. I have the following C++ structure (I’m using qt) :

template <class T>
struct MyStruct
{
    QString name;
    T value;

    QString get_value() {
        if (std::is_same<T, int>::value) return QString::number(value);
        if (std::is_same<T, double>::value) return QString::number(value,'f',8);
        if (std::is_same<T, QString>::value) return value;
    }
}

This code doesn’t compile. I found the explanation here : How do I cast a C++ template parameter?

I understand the problem and why it doesn’t compile, but I can’t find a solution. Is there a way to do this ?

Source: Windows Questions C++

LEAVE A COMMENT