Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ conditional statement to test if variable is a string

I have a C++ method findID that uses templates, and I want to be able to run a condition in this method based on the input type. The template parameter U will either be of type int or type string. I want to run a different condition based on the type of ID.

The code I have is follows:

template <typename S>
template <typename U>
S * findID(U ID){
    for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
        if((*element)->getID() == ID) return *element;
    return NULL;
}

I want my code to do the following:

template <typename S>
    template <typename U>
    S * findID(U ID){

      ***if ID is an int:
        for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
            if((*element)->getID() == ID) return *element;


      ***if ID is a string:

         for (typename vector<S*>::collectionsIter element = collection.begin() ; element != collection.end(); ++element)
            if((*element)->getStringID() == ID) return *element;

       ***else 

        return NULL;


    }

The reason that I want to do this is because I want to be able to compare string variables of ID to the string method of getStringID(), and the int variables of ID to the int method of getID(). In addition I do not want to break these up into separate methods, so I am trying to use templates and these conditions to refactor it into 1 method.

like image 558
MastRofDsastR Avatar asked Jul 19 '26 18:07

MastRofDsastR


2 Answers

Just use 2 overloads:

template <typename S>
S* findID(int ID){
    for (auto* element : collection)
        if (element->getID() == ID) return element;
    return nullptr;
}

template <typename S>
S* findID(const std::string& ID){
    for (auto* element : collection)
        if (element->getStringID() == ID) return element;
    return nullptr;
}
like image 98
Jarod42 Avatar answered Jul 21 '26 08:07

Jarod42


Here's one way to do it using C++17 if constexpr:

struct Foo {
    int id;
    std::string stringId;

    int getId() const { return id; }
    const std::string& getStringId() const { return stringId; }
};

template <typename Cont, typename T>
auto findId(const Cont& c, const T& id) {
    const auto pred = [&id](const auto& x) {
        if constexpr (std::is_convertible_v<T, std::string>)
            return x.getStringId() == id;
        else if constexpr (std::is_convertible_v<T, int>)
            return x.getId() == id;
        else
            static_assert(false, "Unsupported id type.");
        return false;
    };
    const auto findIt = std::find_if(begin(c), end(c), pred);
    return findIt == end(c) ? nullptr : &(*findIt);
}

int main() {
    using namespace std;
    vector<Foo> foos{{1, "1"}, {2, "2"}, {3, "3"}};
    auto foo2Int = findId(foos, 2);
    auto foo2String = findId(foos, "2"s);
    cout << foo2Int->id << ", " << foo2String->stringId << '\n';
}
like image 42
mattnewport Avatar answered Jul 21 '26 08:07

mattnewport



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!