gpt4 book ai didi

c++ - 具有返回抽象类型的函数头是否合法?

转载 作者:可可西里 更新时间:2023-11-01 17:38:55 24 4
gpt4 key购买 nike

我想知道以下内容是否符合 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] 中,非常直接:

An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion.

任何试图做这种事情的代码都是错误的。

关于c++ - 具有返回抽象类型的函数头是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com