mercredi 6 janvier 2016

Restrict function template to specific types?

I want to make function that will make easy to concatenate string. Let suppose I have struct A:

struct A {
    int a;
    double b;
}

Now i want to print like this:

A a = {1, 2.0};
cout << someString + string{"dsdassa"} + a;

or to concat string like this:

string s{"das"};
string s2 = s + A{1, 2.0f};

So i make function like this:

template <typename T>
std::string operator+(std::string & lhs, T && t)
{
    std::cout<< std::endl << "LOG" << '\t' << "operator+(std::string & lhs, T && t)" << std::endl;

    std::string neww(lhs);
    neww += ' ';
    neww += std::to_string(t);
    return neww;
}

For this function to work type T have to have std::to_string function specialized.

If I implement std::to_string for A like this:

 namespace std {
     std::string to_string(A & a)
     {
         return "a = " + std::toString(a.a) + ", b= " + std::to_string(a.b);
     }
 }

examples above will work.

Problem with this is that this will not work if I try to concat 2 strings like this: cout << s + std::string{"blabla"}; because there is no std::to_string for std::string;

I think this could be solved if I somehow could restrict operator+ function to types that have std::to_string.

Is it possible?

Aucun commentaire:

Enregistrer un commentaire