c – 使用enable_if将数字作为函数参数进行匹配

c – 使用enable_if将数字作为函数参数进行匹配,第1张

概述我想使用std :: enable_if来构造匹配数字的构造函数.我尝试了以下代码,但找不到构造函数.主要功能有我想要如何使用它的例子.我应该改变什么来使这项工作? #include <type_traits>#include <string>class A {public: A(const std::wstring&, const std::wstring&) {} 我想使用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:模板参数扣除/替换失败

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

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

以上是内存溢出为你收集整理的c – 使用enable_if将数字作为函数参数进行匹配全部内容,希望文章能够帮你解决c – 使用enable_if将数字作为函数参数进行匹配所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1212006.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存