3

Consider some abstract class A:

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

Suppose I wish to declare a function signature type returning this class:

using Type = A();

Given this code, gcc-4.8.2 failes with error

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

clang-3.3 compiles this nicely.

I tried to google this issue, but failed to find anything usefull. Is this code standard-compliant? If not, what is the reasoning behind forbidding declaring such signature type? I don't see any problems in just a declaration.

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

For those interested in usefullness of such a declaration: I have some factory container that uses a signature like Interface(Arguments...) when adding new factories to learn something about the new factory; the actual returned type is determined based on a separate traits class, parametrised by Interface.

Obviously, I can just separate Interface from signature, but it won't look that nice :(

lisyarus
  • 15,025
  • 3
  • 43
  • 68

2 Answers2

8

Is this code standard-compliant?

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

If not, what is the reasoning behind forbidding declaring such signature type?

A function of that signature can't exist, since it would have to create an object of an abstract class type when it returns.

I don't see any problems in just a declaration.

Indeed, just a declaration would be harmless. But it would also be somewhat useless; it wouldn't refer to any type that can exist.

For your use-case, you could use a reference or pointer as the return type.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

You cannot create an object of an abstract class by definition. Thus a function returning an abstract object just does not make sense. If you still want to return A in some way, you have to return a pointer (that in fact will have to point to an object of a concrete subclass).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I *do not want* to create an object of type `A` or return it from a function. I only want to *declare a signature* returning this type. – lisyarus May 07 '15 at 12:03
  • @lisyarus Hopefully you will return a *pointer* or *reference* to your base class, instead of by value, or you will [slice](https://stackoverflow.com/questions/274626/what-is-object-slicing) instances of derived classes. – Cory Kramer May 07 '15 at 12:04
  • would be intersting to know why I got a down vote on this ... (I gave worse answers without downvotes ;)) – 463035818_is_not_an_ai Jun 11 '15 at 09:12
  • @tobi303 I asked about *declaring a type of a function signature*. Neither about an actual function with this signature, nor about creating an object of an abstract class. So, I think that this answer has nothing to do with the question. – lisyarus Jun 11 '15 at 16:07