mercredi 11 mars 2015

How can I get the most precise representation of a template parameter in c++03?

The code bellow works as expected(it prints out 2.1):



#include <iostream>

template<typename T>
struct number {
T n_;
number(T n)
: n_{n}
{}
};

template<typename A, typename B>
auto operator+(number<A> a, number<B> b) -> number<decltype(a.n_+b.n_)> {
return number<decltype(a.n_+b.n_)>(a.n_+b.n_);
}

int main() {
number<int> a{1};
number<double> b{1.1};
number<double> c{a+b};
std::cout << c.n_ << std::endl;
}


However, it requires C++11. Assuming I'm restricted to C++03, is it possible to achieve the same behaviour? (i.e.: Make the return type of operator+ use the most precise representation for member n_?)


Aucun commentaire:

Enregistrer un commentaire