clang错误与std :: unique_ptr [英] clang error with std::unique_ptr

查看:288
本文介绍了clang错误与std :: unique_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 IList 的基础对象。然后我有 VectorList ,继承 IList

I have base object called IList. Then I have VectorList, that inherits IList.

然后我有这样的功能:

std::unique_ptr<IList> factory(){
    auto vlist = std::make_unique<VectorList>();
    return vlist;
}

这个编译没有问题, gcc ,但 clang 会产生以下错误:

This compiles without problem under gcc, but clang gives following error:

test_file.cc:26:9: error: no viable conversion from 'unique_ptr<VectorList, default_delete<VectorList>>' to
      'unique_ptr<IList, default_delete<IList>>'
        return vlist;

如何正确处理这种错误?

How is proper way to handle this kind of errors?

推荐答案

Clang在这方面仍然遵循C ++ 11的行为。在C ++ 11中,在这种情况下必须使用 std :: move ,因为 vlist 的类型是不同于返回类型,因此,当返回一个左值,尝试它作为右值首先的子句不适用。

It appears (your version of) Clang is still following C++11 behaviour in this regard. In C++11, you had to use std::move in this case, because the type of vlist is different from the return type, and so the clause of "when returning an lvalue, try it as an rvalue first" did not apply.

在C ++ 14中,限制相同类型所需被取消,因此在C ++ 14中,你不需要在返回语句中的 std :: move 。但是如果你需要你的代码来编译你当前的工具链,只需添加它:

In C++14, this restriction of "same types required" was lifted, and so in C++14, you shouldn't need the std::move in the return statement. But if you need your code to compile with your current toolchain, simply add it there:

return std::move(vlist);






确切的C ++ 11措辞是:


The exact C++11 wording was this:


12.8 / 32 当满足复制操作的标准时,事实上,源
对象是一个函数参数,并且要被复制的对象由一个左值指定,重载分辨率为
选择用于复制的构造函数首先被执行,就像该对象由一个右值。 ...

12.8/32 When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. ...

必须满足复制删除的标准(包括相同类型);

The criteria for copy elision (which include "same type") have to be met; they're just slightly extended to cover parameters as well.

在C ++ 14(N4140)中,措辞更宽泛:

In C++14 (N4140), the wording is broader:


12.8 / 32 当满足复制/移动操作的限制条件,但不满足 em>并且要被复制的
对象由一个左值或当返回语句中的表达式是a(可能是
括号) id-expression ,其命名在主体中声明的具有自动存储持续时间的对象或命名为最内层封装函数的
parameter-declaration-clause lambda-expression,
重载解析
选择复制的构造函数首先执行,就好像对象是由右值指定的一样。

12.8/32 When the criteria for elision of a copy/move operation are met, but not for an exception-declaration, and the object to be copied is designated by an lvalue, or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

(强调我)

正如你所看到的, return case。

As you can see, copy elision criteria are no longer required for the return case.

这篇关于clang错误与std :: unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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