是否具有返回抽象类型的函数标头合法

Is having a function header that return an abstract type legal?

本文关键字:函数 类型 返回 抽象 抽象类 是否      更新时间:2023-10-16

我想知道根据C++标准以下内容是合法的:

struct Abstract { virtual ~Abstract() = 0; };
auto get_type() -> Abstract;
// I use `get_type` only to extract the return type.
using MyType = decltype(get_type());

GCC 6.3 接受它,但 Clang 3.9 拒绝它。

但是,如果我这样做:

auto get_type() -> struct Abstract;
struct Abstract { virtual ~Abstract() = 0; };
using MyType = decltype(get_type());

现在两个编译器都接受它。在这种情况下,他们都错了吗?

在[class.abstract]中,非常简单:

抽象类不得用作参数类型、函数返回类型或显式转换的类型。

任何尝试执行此类操作的代码都是格式不正确的。