0

Edit: I know that one can't forward declare typedefs, and there are many qs concluding just that (indeed, I linked to one). But none of them explain why this is disallowed. I'm not looking for a workaround. I want to understand the rationale.

it turns out that you can't forward-declare a using-directive (or typedef). I.e. you can't do this:

Interface:

class Impl;
using Concrete = shared_ptr<Impl>;

Implementation:

using Impl = std::function<Foo(const Foo&)>; //DOES NOT COMPILE

Instead you have to create a one-element class Impl that wraps the std::function.

What's the rationale behind forbidding this? AFAICS all one can do with a forward-declared type like class Impl is to store pointers or references to Impl... and that works even if Impl is later defined to be a primitive like int or a function pointer or et cetera..

(Note that typename Impl doesn't seem to be a valid way of forward declaring a type.)

Mohan
  • 7,302
  • 5
  • 32
  • 55
  • 2
    Different pointer types don't necessarily have the same size or same internal structure. – melpomene Aug 19 '18 at 19:54
  • 2
    Because a `typedef` isn't real. It is a mirage, a fantasy, a figment of one's imagination. It is merely an alias for something else. If someone ever invents a decompiler for C++-compiled code, the decompiler will not be able to decompile any `typedef`s, because accordingly to the rules of physics of our shared universe `typedef`s do not exist. The compiled C++ code has no trace or evidence of any `typedef`s. And you cannot forward-declare something that does not exist. The End. – Sam Varshavchik Aug 19 '18 at 19:54
  • 2
    @SamVarshavchik class names "don't exist" by that logic either, but they can be declared – M.M Aug 20 '18 at 00:10
  • See https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/Upu7b8baIq0 . Short answer is that it makes correct overload resolution, template specialisation impossible. – Mohan Aug 20 '18 at 06:53

0 Answers0