I'm attempting to overload a function depending on whether the passed in sequence container has push_back as a member function.
#include <vector>
#include <forward_list>
#include <list>
#include <array>
#include <iostream>
template<class T, typename std::enable_if_t<std::is_member_function_pointer_v<decltype(&T::push_back)>, int> = 0>
void has_push_back(T p)
{
std::cout << "Has push_back" << std::endl;
}
template<class T, typename std::enable_if_t<!std::is_member_function_pointer_v<decltype(&T::push_back)>, int> = 0>
void has_push_back(T p)
{
std::cout << "No push_back" << std::endl;
}
int main() {
std::vector<int> vec = { 0 };
std::array<int, 1> arr = { 0 };
std::forward_list<int> f_list = { 0 };
has_push_back(vec);
has_push_back(arr);
has_push_back(f_list);
return 0;
}
This results in the following compiler error for each of the calls to has_push_back():
error C2672: 'has_push_back': no matching overloaded function found
error C2783: 'void has_push_back(T)': could not deduce template argument for '__formal'
Expected result:
Has push_back
No push_back
No push_back