【发布时间】:2016-04-20 18:53:36
【问题描述】:
这是我的代码:
#include <memory>
struct A{};
struct B: A {};
std::unique_ptr<A> test()
{
auto p = std::make_unique<B>();
return p;
}
int main(int argc, char **argv)
{
test();
return 0;
}
它不会在 clang 上编译并出现错误:
main.cpp:11:12: error: no viable conversion from returned value of type 'unique_ptr<B, default_delete<B>>' to function return type 'unique_ptr<A, default_delete<A>>'
但是,根据this(相同的情况)它应该。 我是不是误会了什么?
我的命令行(clang 版本 3.7):
clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
【问题讨论】:
-
在 gcc 5.3.0 coliru.stacked-crooked.com/a/73b3835e4eafbc7d 上编译。顺便说一句,您的
A没有虚拟析构函数。 -
这是同一个问题。 gcc 接受它。
-
看来 clang 还没有解决这个问题。它仍然无法在 clang 3.8.0 上编译
-
@milleniumbug:关于析构函数,你是 100% 正确的,但它只是说明问题的快速草稿。