lundi 4 mai 2020

Compilation error for std::is_same usage

I'm trying to use std::is_same::value in my application code. Below is just a sample code I've written to recreate my application code. But I get below compiler error:

error: no matching function for call to ‘Y::getName(bool&, bool&)’

Declaration file:

class X {
    std::string getName(bool a = false, bool b = false) { ... } 
};

class Y {
    std::string getName(bool a = false) { ... }
};

template<typename T>
std::string getMyName(const T &obj, bool a = false, bool b = false);

Application file:

template<typename T>
std::string getMyName(const T &obj, bool a, bool b)
{
    if (std::is_same<T, X>::value)
        return obj->getName(a, b);
    else
        return obj->getName(a);
}

void callerFunc()
{
    ...
    ...
    // X and Y are of pointer types 
    std::string strY = getMyName<Y>(y, true);
    std::string strX = getMyName<X>(x, true, true);
}

I'm using C++11 and can't use later versions. Please advise if there is something wrong with my usage.

Aucun commentaire:

Enregistrer un commentaire