Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was wondering of the following was legal according to the C++ standard:

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 accept it, but Clang 3.9 reject it.

However, if I do this instead:

auto get_type() -> struct Abstract;

struct Abstract { virtual ~Abstract() = 0; };

using MyType = decltype(get_type());

Now both compiler accept it. Are they both wrong in this case?

like image 413
Guillaume Racicot Avatar asked Feb 02 '17 18:02

Guillaume Racicot


1 Answers

In [class.abstract], pretty straightforwardly:

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

Any code that tries to do such a thing is ill-formed.

like image 121
Barry Avatar answered Oct 17 '22 23:10

Barry