mardi 26 juillet 2016

How to use if else along with type initialisation in c++?

Consider a class "myArbPrec" that can be initialised using

myArbPrec("0.1");

(e.g. it represents an arbitrary precision type).

I want my code to be compatible with both myArbPrec and double. The user sets the type with templates.

There are some constants in the template classes that are used in the program such as 0.1. These constants are to be represented in the respective type the user chooses.

My question is: how do I put a constant in the code that is initialised depending on the type chosen? Specifically, for myArbPrec, it should be initialised as myArbPrec("0.1"). For double, it should be initialised as double(0.1).

the problem is that I cannot use myArbPrec(0.1), since this converts first to double, potentially losing precision, and only then to myArbPrec. Therefore, if my template argument is T, I cannot write T(0.1). But because double("0.1") is syntactically incorrect, I can also not use T("0.1").

So far, I tried to adapt this answer to my problem, by writing something like this:

template<typename T>
T atof(const char * c) {
    if (typeid(T) == typeid(double))
        return std::atof(c);
    else
        return T(c);
}

However, this fails because the else branch is still compiled, even if typeid(T) == typeid(double) is always false, which makes this to fail even when T is double.

Aucun commentaire:

Enregistrer un commentaire