Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Avoid extra indentation in nameless namespaces

56 views
Skip to first unread message

Juha Nieminen

unread,
May 22, 2015, 9:35:38 AM5/22/15
to
When declaring a class inside a namespace, especially if it's inside
multiple namespaces, there's a small but slightly annoying problem:
Indentation. In other words:

namespace NS1
{
namespace NS2
{
class MyClass
{
public:
// stuff
};
}
}

The contents of the class will be highly indented, which can be mildly
annoying. You could of course ignore the indentation of namespaces,
but that's bad form (and also annoying with editors that autoindent).
There is, however, a small trick to avoid that. Namely:

namespace NS1 { namespace NS2 { class MyClass; } }

class NS1::NS2::MyClass
{
public:
// stuff
};

I was wondering if the same trick could be used with nameless namespaces.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Victor Bazarov

unread,
May 22, 2015, 10:13:42 AM5/22/15
to
Only if you wrap the contents of the unnamed namespace in another, named
one:

namespace { class FooInAnonymous; }
class FooInAnonymous
{
public:
FooInAnonymous(int) {}
};

int main()
{
FooInAnonymous foo(42); // Error: 'FooInAnonymous' is ambiguous!
}

but
namespace { namespace _ { class FooInAnonymous; } }

class _::FooInAnonymous
{
public:
FooInAnonymous(int) {}
};

int main()
{
_::FooInAnonymous foo(42); // compiles OK, looks ugly.
}

V
--
I do not respond to top-posted replies, please don't ask

asetof...@gmail.com

unread,
May 22, 2015, 10:28:00 AM5/22/15
to

Ian Collins

unread,
May 22, 2015, 3:42:43 PM5/22/15
to
Juha Nieminen wrote:
> When declaring a class inside a namespace, especially if it's inside
> multiple namespaces, there's a small but slightly annoying problem:
> Indentation. In other words:
>
> namespace NS1
> {
> namespace NS2
> {
> class MyClass
> {
> public:
> // stuff
> };
> }
> }
>
> The contents of the class will be highly indented, which can be mildly
> annoying. You could of course ignore the indentation of namespaces,
> but that's bad form (and also annoying with editors that autoindent).
> There is, however, a small trick to avoid that. Namely:

Google's coding standard has a do not indent namespaces rule and most
editors I've seen can be configured to follow that rule.

--
Ian Collins

Juha Nieminen

unread,
May 25, 2015, 5:30:25 AM5/25/15
to
Ian Collins <ian-...@hotmail.com> wrote:
> Google's coding standard has a do not indent namespaces rule and most
> editors I've seen can be configured to follow that rule.

Google can eat my shorts.

{} is a block, and its contents are indented.

woodb...@gmail.com

unread,
May 25, 2015, 4:38:09 PM5/25/15
to
On Monday, May 25, 2015 at 4:30:25 AM UTC-5, Juha Nieminen wrote:
> Ian Collins <ian-...@hotmail.com> wrote:
> > Google's coding standard has a do not indent namespaces rule and most
> > editors I've seen can be configured to follow that rule.
>
> Google can eat my shorts.
>
> {} is a block, and its contents are indented.
>

I prefer https://duckduckgo.com to Google.

But I don't indent namespaces either. I'm big on
indentation in general, but in this case don't seem
to miss it.

Brian
Ebenezer Enterprises - "For a righteous man falls seven
times, and rises again, but the wicked stumble in time
of calamity." Proverbs 24:16

http://webEbenezer.net

Jorgen Grahn

unread,
May 28, 2015, 9:04:19 AM5/28/15
to
On Mon, 2015-05-25, Juha Nieminen wrote:
> Ian Collins <ian-...@hotmail.com> wrote:
>> Google's coding standard has a do not indent namespaces rule and most
>> editors I've seen can be configured to follow that rule.
>
> Google can eat my shorts.
>
> {} is a block, and its contents are indented.

Agree. For a few months ten years back I tried not to indent, but in
the end I went with the flow, and I now think it's better to indent.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages