Function signature returning an abstract class

Consider some abstract class A

:

class A
{
    virtual void f() = 0;
};

      

Suppose I want to declare a function signature type that returns this class:

using Type = A();

      

Given this code, it gcc-4.8.2

exits with an error

error: ‘type name’ declared as function returning an abstract class type

      

clang-3.3

easy to compile.

I tried to solve this problem but didn't find anything useful. Is this code standard? If not, what is the reason for prohibiting the announcement of this type of signature? I don't see any problem just in the ad.

Disclaimer: I am NOT going to create instances of this type, I am just interested in declaring the signature described.

For those interested in the usefulness of such a declaration: I have a factory container that uses a type signature Interface(Arguments...)

when adding new factories to learn something about the new factory; the actual return type is determined based on the individual feature class parameterized Interface

.

Obviously I can just separate Interface

from the signature, but it won't look that pretty :(

+3


source to share


2 answers


Is this code standard?

Not. According to C ++ 11 [class.abstract] / 3, "An abstract class should not be used as a parameter type, as a function return type , or as an explicit conversion type."

If not, what is the reason for prohibiting the announcement of this type of signature?

A function of this signature cannot exist as it will have to create an object of the abstract class type when it returns.



I don't see any problem just in the ad.

Indeed, a simple announcement would be harmless. But that would also be useless; it will not refer to any type that may exist.

For your use case, you can use reference or pointer as return type.

+8


source


You cannot create an object of an abstract class by definition. So a function that returns an abstract object just doesn't make sense. If you still want to return in some way A

, you need to return a pointer (which should actually point to an object of a particular subclass).



0


source







All Articles