我有四个功能:
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_alloc
和 Argument = 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/