c++ - 类型推导失败:派生模板的 shared_ptr 到基模板作为函数参数
问题描述
这里我声明了两个模板类:A和B,B派生自A:
template<typename T>
class A {
public:
int a;
T t;
};
template<typename T>
class B : public A<T> {
public:
int b;
};
我做了一个shared_ptr<B<T>>
并将其分配给shared_ptr<A<T>>
,没关系:
auto b = std::make_shared<B<std::string>>();
std::shared_ptr<A<std::string>> a = b;
这里我声明了一个模板函数 accept shared_ptr A<T>
:
template<typename T>
void proc(std::shared_ptr<A<T>> &a) {
std::cout << a->a << std::endl;
}
它接受a
作为参数,但拒绝b
:
proc<std::string>(a); // OK
proc<std::string>(b); // template argument deduction/substitution failed
// cannot convert 'b' (type 'std::shared_ptr<B<std::__cxx11::basic_string<char> > >') to type 'std::shared_ptr<A<std::__cxx11::basic_string<char> > >&'
我使用 g++ 作为 -std=c++11 的编译器。
这个错误给我带来了很多问题,我该如何优雅地解决这个问题?
解决方案
给定proc<std::string>(b);
,b
需要转换为std::shared_ptr<A<std::string>>
. std::shared_ptr<A<std::string>>
这意味着将构建一个临时对象,然后将其传递给proc
. 的参数类型proc
是对非常量的左值引用,即std::shared_ptr<A<T>> &
不能绑定到临时变量。
您可以将参数类型更改为对 const 的左值引用,这可以绑定到临时变量。例如
template<typename T>
void proc(const std::shared_ptr<A<T>> &a) {
// ^^^^^
std::cout << a->a << std::endl;
}
推荐阅读
- javascript - 在 JavaScript/Nodejs 中动态处理未定义变量的最佳实践
- android - 没有额外信息的 RemoteServiceException
- sql - 将 Access 数据库复制到 SQL Server 正常但参数输入查询失败
- java - 如何在 Java 中使用 PDFPTable 从上到下将 2 个表添加到单个表中
- python - 不能根据来源的内容写日期
- xpages - IBM Designer 客户端在 Notes 客户端中启动表单和视图!CLFRJ0010E 有时
- c# - 如何摆脱“Animator 没有播放 AnimtorController”警告?
- iis - 使用 IIS7 URL Rewrite 将虚拟目录的请求转发到内部 Web 服务器
- azure-iot-sdk - 未链接到 libcurl 的示例
- javascript - 围绕 javascript/html 数据选项的 if 语句