如何将c-函数参数匹配改写为使用enable_if进行数字类型的长尾?

2026-04-16 19:353阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计261个文字,预计阅读时间需要2分钟。

如何将c-函数参数匹配改写为使用enable_if进行数字类型的长尾?

我想使用`std::enable_if`来构造适配数字的构造函数。我尝试了以下代码,但没有找到构造函数。主要功能是我想要如何使用它的例子。我应该改变什么来实现这一功能?

我想使用std :: enable_if来构造匹配数字的构造函数.我尝试了以下代码,但找不到构造函数.主要功能有我想要如何使用它的例子.我应该改变什么来使这项工作?

#include <type_traits> #include <string> class A { public: A(const std::wstring&, const std::wstring&) {} template<typename T> A(const std::wstring& n, typename std::enable_if<std::is_arithmetic<T>::value, T>::type v) {} }; int main() { A(L"n", 1234); // does not compile A(L"n", 2.7); // does not compile A(L"n", L"n"); // compiles return 0; }

错误on Ideone:模板参数扣除/替换失败

如何将c-函数参数匹配改写为使用enable_if进行数字类型的长尾?

你的T不可扣除(由于:: type),一种方法是:

template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> A(const std::wstring& n, T v) {}

本文共计261个文字,预计阅读时间需要2分钟。

如何将c-函数参数匹配改写为使用enable_if进行数字类型的长尾?

我想使用`std::enable_if`来构造适配数字的构造函数。我尝试了以下代码,但没有找到构造函数。主要功能是我想要如何使用它的例子。我应该改变什么来实现这一功能?

我想使用std :: enable_if来构造匹配数字的构造函数.我尝试了以下代码,但找不到构造函数.主要功能有我想要如何使用它的例子.我应该改变什么来使这项工作?

#include <type_traits> #include <string> class A { public: A(const std::wstring&, const std::wstring&) {} template<typename T> A(const std::wstring& n, typename std::enable_if<std::is_arithmetic<T>::value, T>::type v) {} }; int main() { A(L"n", 1234); // does not compile A(L"n", 2.7); // does not compile A(L"n", L"n"); // compiles return 0; }

错误on Ideone:模板参数扣除/替换失败

如何将c-函数参数匹配改写为使用enable_if进行数字类型的长尾?

你的T不可扣除(由于:: type),一种方法是:

template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> A(const std::wstring& n, T v) {}