输入具有特定成员函数的重载函数

发布于 2025-01-10 12:44:53 字数 1181 浏览 0 评论 0原文

我试图根据传入的序列容器是否将 push_back 作为成员函数来重载函数。

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

这会导致每次调用 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'

预期结果:

Has push_back
No push_back
No push_back

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2025-01-17 12:44:54

您可以使用 表达式 SFINAE 来实现:

#include <iostream>
#include <vector>
#include <array>
#include <forward_list>

void has_push_back(...)
{
  std::cout << "No push_back" << std::endl;
}

template<class T>
auto has_push_back(T&& t) 
  -> decltype(t.push_back(t.front()), void()) 
  // ^^ expression SFINAE, only considered 
  // if the whole expression within decltype is valid
{
  std::cout << "Has 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;
}

结果:

Has push_back
No push_back
No push_back

https://godbolt.org/z/dbzafs1nY

push_back 是模板或重载时,&T::push_back 的格式可能不正确。相反,我检查使用 t.front()push_back 的调用是否有效。当然,这也要求类型有合适的 front 成员。

You could use expression SFINAE for this:

#include <iostream>
#include <vector>
#include <array>
#include <forward_list>

void has_push_back(...)
{
  std::cout << "No push_back" << std::endl;
}

template<class T>
auto has_push_back(T&& t) 
  -> decltype(t.push_back(t.front()), void()) 
  // ^^ expression SFINAE, only considered 
  // if the whole expression within decltype is valid
{
  std::cout << "Has 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;
}

Result:

Has push_back
No push_back
No push_back

https://godbolt.org/z/dbzafs1nY

&T::push_back may not be well formed when push_back is a template or overloaded. Instead, I check if a call with t.front() to push_back is valid or not. Of course this also requires the type to have a suitable front member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文