- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的代码中,我使用了模板化图像类 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
的精神,是否可以告诉编译器该字段没有专门化?告诉编译器一个字段是一个类型的关键字?
最佳答案
What is causing the compiler error?
您(仅)在非推导上下文中使用 T
:nested-name-specifier。也就是说,您将 T
放在一个仅指定类型所在位置的名称中。编译器无法理解您的实际意图,因此必须尝试很多 T
。
Is it possible to use the signature of
function1()
without having to manually specify the template argument?
不是真的。如果您想要一种更简洁的方式来引用指向图像的智能指针,您可以使用别名模板:
template <typename T>
using ImagePtr = std::shared_ptr<Image<T>>;
然后这样写function1
:
template <typename U>
void function1(ImagePtr<U> p) {}
关于c++ - 依赖类型 : Template argument deduction failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541008/
我试图将要处理的元素类型限制为 std::array> , 但 N 的模板替换失败。 main.cpp:10:1: note: template argument deduction/substi
在我的代码中,我使用了模板化图像类 Image结合 std::shared_ptr .这些图像指针应该传递给各种图像处理函数,其中一些函数与图像类型无关。考虑以下 Image 的定义和两个处理函数 f
在 C++20 中,根据建议 Familiar template syntax for generic lambdas , 下面的代码正确推导出类型 T: auto lamTest = [](std:
在使用 GCC 4.7.2 和 Clang 3.1 编译一些 C++11 代码时,我遇到了一个问题,即 Clang 无法推断出 GCC 成功的模板参数。在更抽象的形式中,代码如下所示: src/tes
C++ 中有一个很好的特性,您可以说该函数的返回类型为“auto”,编译器会找出它。但是,如果我在出错时返回一个指针和 nullptr 怎么办?不知何故,编译器无法推断出正确的类型并给出错误。 在下面
我似乎找不到正确的方法来实现它,这似乎是最接近正确的方法,但我得到一个模板参数推导错误。谁能指出我哪里出错了? 我正在尝试将算术功能添加到 std::variant无需std::get第一的: #in
我写了下面的测试: #include #include #include #include #include // Works with std::function ! //std::fun
以下程序(使用标准随机数引擎)给出“模板参数推导/替换失败”编译错误: http://coliru.stacked-crooked.com/a/e3c3ac933ed7b958 /// LCE1.cp
我正在尝试创建一个函数,该函数接受任何类型的可变数量的参数,但即使是我制作的简单示例也会出现错误 #include #include template void callFunction(cons
当我尝试逐行读取命令行的输出时出现此错误: std::string exec(const char* cmd) { FILE* pipe = popen(cmd, "r"); if (!pip
“类模板的模板参数推导” 提案 ( P0091R2 ) 包含以下示例: template struct X { X(Ts...) }; X x1{1}; // OK X X x11; // OK X<
在temp.deduct.partial#8 ,有一个例子: template void f(Args... args); // #1 template void
简单的例子: const mapStateToProps = (state: State) => ({ error: state.error, // error: ?string }) type
我设计了一个对象,它接受一个函数及其参数,并将函数的返回值保存在对象中以供稍后检索。 我的目标是为这个对象创建一个推导指南,允许我在对象的构造函数中省略函数的返回类型。 #include templ
我是 C++ 的新手。 我写了一个非常简单的程序,如下所示 #include using namespace std; class index { protected: int count;
我编写了以下代码来测试模板特化。有没有办法让指针衰减到类型,以便我可以使用模板参数推导来获取我的 M 和 N,这样我就可以获得二维数组的行和列?我知道我可以为二维数组 vector 的 vector
我正在使用 gcc 8.0.0 201706 尝试一个关于 1z 的推导指南的示例(无法使用 clang 5.0.5 编译代码)。 namespace std { template std::
在这里,我实现了一个模板化函数和一个模板化 Lambda。我已经开始探索 C++14 功能,但不确定以下 lambda 有什么问题。有什么建议吗? #include #include #inclu
在英语语义中,“type deduction”等于“type inferring”吗? 我不确定 这只是不同语言设计者选择的成语偏好,或者 计算机科学给出了严格的“类型推导”定义, 哪个不是“类型推断
这里是代码示例。 a. int ii = 0; b. const int ci = ii; c. auto e = &ci; --> e is const int * d. auto &f = 42;
我是一名优秀的程序员,十分优秀!