mercredi 1 avril 2015

Does replacing template with namespace cause serious trouble for my code

If I have a code similar to this:



#include <iostream>

template<typename T>
class compare
{
public:
bool comparison(T a,T b)
{
return (a>b);
}

};

int main()
{

int a=6,b=5;
compare<int> cmp_i;
std::cout<<cmp_i.comparison(a,b)<<std::endl;

double c=7.0,d=7.5;
compare<double> cmp_d;
std::cout<<cmp_d.comparison(c,d)<<std::endl;

return 0;
}


and since I am very inclined to remove template from my program I use namespaces to solve the problem:


main.cpp:



#include <iostream>

namespace int_space
{
typedef int T;
#include "base.hpp"
}

namespace double_space
{
typedef double T;
#include "base.hpp"
}

int main()
{

int a=6,b=5;
int_space::compare cmp_i;
std::cout<<cmp_i.comparison(a,b)<<std::endl;

double c=7.0,d=7.5;
double_space::compare cmp_d;
std::cout<<cmp_d.comparison(c,d)<<std::endl;

return 0;
}


base.hpp:



class compare
{
public:
bool comparison(T a,T b)
{
return (a>b);
}
};


Both do the same thing but the second one does not use any template.


I wonder if this trick makes any serious problem for the future of my code when extending.


Aucun commentaire:

Enregistrer un commentaire