lundi 5 août 2019

automatic resolve member function with const postfix

Inside my class I have 2 algorithms that should be applied depending if container supports lower_bound.

template <class T>
class SmartContainerHandler{
....
template <class Q=T>
typename std::enable_if< std::is_member_function_pointer<decltype(&Q::lower_bound)>::value >::type
foo_impl(int k, double _ = 0)
{
    auto r = _t.lower_bound(k);
    std::cout << "has lower_bound" << (r == _t.end() ? -1 : *r) << std::endl;
}
template <class Q=T>
typename std::enable_if<! std::is_member_function_pointer<decltype(&Q::lower_bound)>::value >::type
foo_impl(int k)
{
    std::cout << << "no lower_bound, just a key:" << k <<  std:: endl;
}

For example I would like to instantiate either with SmartContainerHandler<std::set<int> > or SmartContainerHandler<std::vector<int> >

My problem that lower_bound has 2 signatures

template< class K > iterator lower_bound(const K& x);
template< class K > const_iterator lower_bound(const K& x) const;

(just try to compile this without success auto m = &std::set<int>::lower_bound)

In my particular case I don't care about const signature, but in sake of more generic question: How to check method exists if provided 2 or more signatures?

Aucun commentaire:

Enregistrer un commentaire