简体   繁体   中英

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.

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;
}

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';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM