Variadic list of pointers to data members

118 views Asked by At

Consider this working code. The function searchByDataMember uses a pointer to data member as argument to search a value among a container.

#include <iostream>
#include <list>
#include <string>

template <typename Container, typename T, typename DataPtr>
typename Container::value_type searchByDataMember (const Container& container, const T& t,
    DataPtr ptr) {
    for (const typename Container::value_type& x : container) {
        if (x->*ptr == t)
            return x;
    }
    return typename Container::value_type{};
}

struct Object {
    int ID, value;
    std::string name;
    Object (int i, int v, const std::string& n) : ID(i), value(v), name(n) {}
};

std::list<Object*> objects { new Object(5,6,"Sam"), new Object(11,7,"Mark"),
    new Object(9,12,"Rob"), new Object(2,11,"Tom"), new Object(15,16,"John") };

int main() {
    const Object* object = searchByDataMember (objects, 11, &Object::value);
    std::cout << object->name << '\n';  // Tom
}

So how to extend the above to using a variadic list of pointers to data members as arguments, in the event that a data member pointed to itself has data members to search? For example,

#include <iostream>
#include <list>
#include <string>

template <typename Container, typename T, typename... DataPtrs>
typename Container::value_type searchByDataMember (const Container& container, const T& t,
    DataPtrs... ptrs) {
        // What to put here???
}

struct Thing {
    int ID, value;
    std::string name;
    Thing (int i, int v, const std::string& n) : ID(i), value(v), name(n) {}
};

struct Object {
    int rank;
    Thing* thing;
    Object (int r, Thing* t) : rank(r), thing(t) {}
};

std::list<Object*> objects { new Object(8, new Thing(5,6,"Sam")), new Object(2, new Thing(11,7,"Mark")),
    new Object(1, new Thing(9,12,"Rob")), new Object(9, new Thing(2,11,"Tom"))};

int main() {
    // The desired syntax.
//  const Object* object = searchByDataMember (objects, 11, &Object::thing, &Thing::value);
//  std::cout << object->thing->name << '\n';  // Tom (the desired output)
}

So here we wish to search among the container objects for the Object* that has a Thing* data member whose value data member is 11, which is the Object* that has "Tom". There is to be no limit on how big a chain of pointers to data members can be passed into searchByDataMember.

1

There are 1 answers

1
Jarod42 On BEST ANSWER

You need a way to apply operator ->* in succession:

template <typename T, typename MPtr>
auto arrow(T* obj, MPtr mptr)
{
    return obj->*mptr;
}

template <typename T, typename MPtr, typename ... MPtrs>
auto arrow(T* obj, MPtr mptr, MPtrs... mptrs)
{
    return arrow(obj->*mptr, mptrs...);
}

Then your search function is simple, something like: (I prefer to return iterator over value btw)

template <typename Container, typename T, typename... DataPtrs>
auto searchByDataMember (const Container& container, const T& t, DataPtrs... ptrs)
{
    return std::find_if(std::begin(container), std::end(container),
                [&](const auto&e) {
                    return arrow(e, ptrs...) == t;
                });
}

Demo