c++ - 对函数的模糊调用

标签 c++ overloading

我有四个功能:

template<class Exception,class Argument>
 void allocate_help(const Argument& arg,Int2Type<true>)const;

 template<class Exception,class Argument>
 std::nullptr_t allocate_help(const Argument& arg,Int2Type<false>)const;

 template<class Exception>
 void allocate_help(const Exception& ex,Int2Type<true>)const;

 template<class Exception>
 std::nullptr_t allocate_help(const Exception& ex,Int2Type<false>)const;

但是当我打电话时:

allocate_help<std::bad_alloc>(e,Int2Type<true>()); //here e is of a std::bad_alloc type 

我收到一个错误:
错误 3 error C2668:对重载函数的调用不明确 为什么?

最佳答案

因为您的电话同时匹配:

template<class Exception,class Argument>
 void allocate_help(const Argument& arg,Int2Type<true>)const;

使用 Exception = std::bad_allocArgument = std::bad_alloc(Argument 自动推导),并且:

template<class Exception>
 void allocate_help(const Exception& ex,Int2Type<true>)const;

使用 Exception = std::bad_alloc。因此调用的含糊不清。

另外我认为你的编译器应该在错误行之后输出所有匹配的函数,所以你可以自己回答你的问题。

关于c++ - 对函数的模糊调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4326864/

相关文章:

scala - 对 Scala 中重载方法的引用

c++ - Boost::Thread 函数导致嵌入式 ARM 上的段错误

java - 如何避免创建大量重载方法?

c++ - 函数重载不适用于 C++ 中的模板?

c++ - 当我重载函数时,函数指针是否起作用?

c++ - 位于不同命名空间时运算符的重载不明确

c# - 使用 VC++ MFC 对话从数据库中检索数据

java - eclipse - 无缝调试 Java 和 C++ 代码

c++ - Oracle OCI 不查询缓存结果

c++ - std :map 中的浮点键