c++ - 用新的函数声明语法覆盖

标签 c++ c++11

<分区>

鉴于 C++11 中引入的新函数声明语法,很容易将成员函数从:

std::unique_ptr<...> operator()() const;

auto operator()() const -> std::unique_ptr<...>;

我相信要将覆盖添加到第一个你会写:

std::uniqe_ptr<...> operator()() const override;

当我写的时候:

auto operator()() const -> std::unique_ptr<...> override;

Clang 给出错误:

error: type-id cannot have a name
auto operator()() const -> std::unique_ptr<...> override
                                                ^~~~~~~~

当我写的时候:

auto operator()() const override -> std::unique_ptr<...>;

Clang 给出错误:

error: 'auto' return without trailing return type
auto operator()() const override -> std::unique_ptr<...>;
^

您会在新语法的什么地方放置重写?

最佳答案

根据语法,override 关键字应该在尾随返回类型之后,即,您将使用

auto operator()() -> std::unique_ptr<int> override;

用 gcc 的最新快照尝试它可以工作,而即使是最新版本的 clang 也会失败。我会说这是 clang 实现中的一个错误。

该标准的相关引用是 8.4.1 [dcl.fct.def.general] 第 1 段:

Function definitions have the form

function-definition:
    attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt   function-body
function-body:
    ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;

virt-specifier-seqoptfinal 和/或override 组成。

关于c++ - 用新的函数声明语法覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18861184/

相关文章:

具有类特定参数的 C++ 虚方法

c++ - 如何修复基于 cpp 的具有 2 种不同大小的 C++ 结构?

c++ - 如何使用CMake更改构建机器类型

c++ - 具有非类型模板参数的方法

c++ - 为什么数组的类型推导优先于对数组的引用优先指针?

C++ 重新定义变量,全局命名空间被污染,我不知道为什么

c++ - C++中std::multiset::count的时间复杂度是多少?

c++ - 抛出 "bad_alloc"的最常见原因是什么?

c++ - 是否可以使函数模板从函数引用中获取 `decltype`?

c++ - 错误 : use of deleted function bool regex_match with gcc 5. 2.0