Already Discussed? Proposal of Simplified Class Forward Declarations using Namespaces.

155 views
Skip to first unread message

lucky.r...@gmail.com

unread,
Feb 27, 2019, 8:28:40 AM2/27/19
to ISO C++ Standard - Future Proposals

Hi

I would like to create a proposal to simplify class forward declarations using namespaces.

Before I invest time into the proposal, I did like to know if this kind of proposal was already discussed and/or rejected. My internet search did not got any result, but this may be just the case because I did not found the right keywords for my search.

My core proposal is actually quite simple. If one is using libraries and namespaces, a forward declaration of a class in a namespace is a common thing

namespace ex {
class Foo;
class Bar;
}

class Example {
  ex
::Foo *_foo;
  ex
::Bar *_bar;
};


With nested namespace, this code gets worse:

namespace ex {
namespace foo {
class Foo;
}
namespace bar {
class Bar;
}
}

class Example {
  ex
::foo::Foo *_foo;
  ex
::bar::Bar *_bar;
};


If namespaces in forward declarations would be allowed, it would simplify the code:

class ::ex::foo::Foo;
class ::ex::bar::Bar;

class Example {
  ex
::foo::Foo *_foo;
  ex
::bar::Bar *_bar;
};


Was this feature ever discussed? Was it rejected, and why?

All the best,
LR

Dejan Milosavljevic

unread,
Feb 28, 2019, 10:44:54 AM2/28/19
to std-pr...@isocpp.org
In forward declaration it is not clear are foo or bar is namespace or class. 

namespace ex::foo; //!< foo is namespace an this imply that ex is namespace too
namespace ex::bar;
class
::ex::foo::Foo;

class ::ex::bar::Bar;

class Example {
  ex
::foo::Foo *_foo;
  ex
::bar::Bar *_bar;
};
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/59af2837-0237-4cfa-8b89-dafc1b7a77c1%40isocpp.org.

tobias....@gmail.com

unread,
Feb 28, 2019, 11:08:30 AM2/28/19
to ISO C++ Standard - Future Proposals

You are absolutely right!

A solution would be a forward declaration of namespaces, as you shown in your code.
This would still remove the requirements for the brackets around the class definitions and allow a simpler syntax.

All the best,
LR

Victor Dyachenko

unread,
Mar 1, 2019, 4:36:57 AM3/1/19
to ISO C++ Standard - Future Proposals
Forwarding namespaces is an orthogonal task. Why do we need it at all?

namespace ex::foo {}

And it's legal already.

About allowing namespaces in forward declarations I think that it's a good idea. Missing this feature. It would be very handy.

Gašper Ažman

unread,
Mar 1, 2019, 4:46:31 AM3/1/19
to std-pr...@isocpp.org
Namespaces and class names are in different... Name spaces. Even if you forward declare a namespace it's not a given a prefix is in fact a namespace. What we have is as good as it gets.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Message has been deleted

Gašper Ažman

unread,
Mar 1, 2019, 4:55:50 AM3/1/19
to std-pr...@isocpp.org
I seem to have been wrong. Seems like you can't declare a type with the same name as a namespace. I was writing from a train, should have checked on godbolt first :).

On Fri, Mar 1, 2019, 09:51 <tobias....@gmail.com> wrote:

Even if you forward declare a namespace it's not a given a prefix is in fact a namespace.

Can you explain this in detail?

I would assume, if I declare a namespace:

namespace foo {}

In this scope, "foo" would be recognised as a namespace, so the following forward declaration would be clear for the compiler.

class foo::Example;

What did I miss? 
Reply all
Reply to author
Forward
0 new messages