在我的代码中,我使用模板图像类Image<T>
与std::shared_ptr
的组合。这些图像指针应该被传递给各种图像处理功能,其中一些独立于图像类型。考虑Image<T>
的以下定义,以及两个处理函数function1()
和function2()
。相关类型:模板参数扣除失败
#include <memory>
template <typename T>
struct Image
{
typedef std::shared_ptr<Image<T>> Ptr;
};
template <typename T>
void function1 (typename Image<T>::Ptr image) {}
template <typename T>
void function2 (std::shared_ptr<Image<T>> image) {}
虽然function1()
和function2()
有效地具有相同的签名,function1()
更容易阅读和隐藏的指针是如何实现的细节。但是,我无法在不明确指定模板类型的情况下调用function1()
。请看下面的代码:
int main (void)
{
Image<int>::Ptr image = std::make_shared<Image<int>>();
function1(image); // Does NOT compile
function1<int>(image); // Does compile
function2(image); // Does compile
return 0;
}
当第一次调用导致编译错误:
example.cc: In function 'int main()':
example.cc:18:19: error: no matching function for call to 'function1(MyClass<int>::Ptr&)'
example.cc:18:19: note: candidate is:
example.cc:10:6: note: template<class T> void function1(typename MyClass<T>::Ptr)
example.cc:10:6: note: template argument deduction/substitution failed:
example.cc:18:19: note: couldn't deduce template parameter 'T'
我的问题是:是否有可能使用的function1()
签名,而无需手动指定模板参数?什么导致编译器错误?
我怀疑问题是由于Image<T>::Ptr
是一个依赖类型。因此编译器在编译时无法知道该字段的确切定义。是否有可能告诉编译器这个字段没有专门化,typename
关键字的精神告诉编译器一个字段是一个类型?
通过“不是真的”,你意思是“不”对吧? – Barry
@Barry那么,我还没有检查过,如果VC++中的疯狂错误使它在那里工作,但是 - 没有。 – Columbo