相关类型:模板参数推导失败 [英] Dependent Types: Template argument deduction failed

查看:94
本文介绍了相关类型:模板参数推导失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我结合使用了模板图像类 Image< T> std :: shared_ptr 。这些图像指针应该传递给各种图像处理功能,其中一些功能与图像类型无关。考虑以下 Image< T> 的定义,以及两个处理函数 function1()

In my code I use a templated image class Image<T> in combination with std::shared_ptr. These image pointers are supposed to be passed to various image processing functions, some of which are independent of the image type. Consider the following definition of Image<T>, and two processing functions function1() and 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()时遇到麻烦。考虑下面的代码:

While function1() and function2() effectively have the same signature, function1() is easier to read and hides the details of how the pointer is realized. However, I am having trouble calling function1() without explicitly specifying the template type. Consider the following code:

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;
}

第一次调用会导致编译错误的地方:

Where the first call leads to the compile error:

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()无需手动指定模板参数?是什么导致编译器错误?

My question is the following: Is it possible to use the signature of function1() without having to manually specify the template argument? What is causing the compiler error?

我怀疑问题是由 Image< T> :: Ptr 是从属类型。因此,编译器在编译时无法知道该字段的确切定义。可以按照 typename 关键字的精神,告诉编译器该字段没有类型吗?

I suspect the problem is caused by the fact that Image<T>::Ptr is a dependent type. Thus the compiler cannot know the exact definition of this field at compile time. Is it possible to tell the compiler there will be no specializations of this field, in the spirit of the typename keyword which tells the compiler that a field is a type?

推荐答案


是什么原因导致编译器错误?

What is causing the compiler error?

您(仅)在非推论上下文中使用 T 嵌套名称说明 。也就是说,您将 T 放在仅指定类型所在位置的名称内。编译器无法理解您的实际意图,因此必须尝试很多 T

You're (solely) using T in a non-deduced context: nested-name-specifiers. That is, you put T inside a name that merely specifies where the type lies within. The compiler cannot understand your actual intention and would have to try a lot of T's.


是否可以使用 function1()的签名而不必手动指定模板参数?

Is it possible to use the signature of function1() without having to manually specify the template argument?

不是。如果您想以更简洁的方式引用指向图像的智能指针,则可以使用别名模板:

Not really. If you want a more concise way of referring to a smart pointer to an Image, you can use an alias template though:

template <typename T>
using ImagePtr = std::shared_ptr<Image<T>>;

然后编写 function1

template <typename U>
void function1(ImagePtr<U> p) {}

这篇关于相关类型:模板参数推导失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆