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

C++ namespace question

40 views
Skip to first unread message

David Brown

unread,
Jun 14, 2021, 8:30:13 AM6/14/21
to

If I am inside a namespace, is there a way to declare a function as
being outside the namespace without closing and reopening it? I.e., can
I do:


namespace N {

int f(int x) { return x; }

}

int g(int x) { return x; }


namespace N {

int h(int x) { return x; }

}

/without/ having to have "namespace N" twice?

Bonita Montero

unread,
Jun 14, 2021, 9:00:24 AM6/14/21
to
Do you think about sth. like:

namespace fuck
{
void fn();
}

void fuck::fn()
{
}

David Brown

unread,
Jun 14, 2021, 9:32:06 AM6/14/21
to
No.

The aim is to declare a function that is outside the namespace, but to
be able to do so from within the namespace.

Öö Tiib

unread,
Jun 14, 2021, 1:05:56 PM6/14/21
to
On Monday, 14 June 2021 at 15:30:13 UTC+3, David Brown wrote:
> If I am inside a namespace, is there a way to declare a function as
> being outside the namespace without closing and reopening it?

Can be that it is <https://en.wikipedia.org/wiki/XY_problem> as it is unclear
why you do not want to define whatever functions of whatever namespace
within that namespace. Repeating namespace N twice is not needed:

namespace N {

int f(int x) { return x; }

int h(int x) { return x; }

David Brown

unread,
Jun 14, 2021, 1:43:35 PM6/14/21
to
Oh, I know I can do /that/. I could also define g() before the
"namespace N" at the start - and of course I could write it as I did
originally. But in my code, the global namespace function "g" fits
naturally along with other code that is within the namespace of the
model. If there is a syntax unknown to me that could be used declare
"g" as being outside the namespace, it would be neater in my code structure.


Öö Tiib

unread,
Jun 14, 2021, 2:28:44 PM6/14/21
to
Probably there are no such way.
I only have functions with internal linkage in global namespace as code
bases tend to be huge lately. Why you need it to be in global namespace?
I prefer to qualify explicitly instead of wrapping into namespace N {}.
When it is something long like depths::of_various::spaces then I
can shorten it with alias first:

namespace N = depths::of_various::spaces;

static int g(int x);

int N::f(int x) { return x; }

int g(int x) { return x; }

int N::h(int x) { return x; }




Louis Krupp

unread,
Jun 14, 2021, 4:26:52 PM6/14/21
to
For what it's worth, the obvious attempt at a solution:

===
namespace n
{
    void   nf1() {}
    void ::gf2() {}
    void   nf3() {}
}
===

doesn't work with g++:

===
:4:16: error: declaration of ‘void gf2()’ not in a namespace surrounding
‘::’
    4 |     void ::gf2() {}
      |                ^
:4:16: error: ‘void gf2()’ should have been declared inside ‘::’
===

But you've probably tried that.

Louis

Paavo Helde

unread,
Jun 14, 2021, 4:28:20 PM6/14/21
to
If g "fits naturally" between f and h then they all should be in the
same namespace. This does not prohibit you from having it also in the
global namespace:

namespace N {
int f(int x) { return x; }
int g(int x) { return x; }
int h(int x) { return x; }
}

int global_g(int x) {
return N::g(x);
}

.. OR ..

using N::g;




David Brown

unread,
Jun 14, 2021, 5:07:39 PM6/14/21
to
On 14/06/2021 20:28, Öö Tiib wrote:
> On Monday, 14 June 2021 at 20:43:35 UTC+3, David Brown wrote:
>> On 14/06/2021 19:05, Öö Tiib wrote:
>>> On Monday, 14 June 2021 at 15:30:13 UTC+3, David Brown wrote:
>>>> If I am inside a namespace, is there a way to declare a function as
>>>> being outside the namespace without closing and reopening it?
>>>
>>> Can be that it is <https://en.wikipedia.org/wiki/XY_problem> as it is unclear
>>> why you do not want to define whatever functions of whatever namespace
>>> within that namespace. Repeating namespace N twice is not needed:
>>>
>>> namespace N {
>>>
>>> int f(int x) { return x; }
>>>
>>> int h(int x) { return x; }
>>>
>>> }
>>>
>>> int g(int x) { return x; }
>>>
>> Oh, I know I can do /that/. I could also define g() before the
>> "namespace N" at the start - and of course I could write it as I did
>> originally. But in my code, the global namespace function "g" fits
>> naturally along with other code that is within the namespace of the
>> model. If there is a syntax unknown to me that could be used declare
>> "g" as being outside the namespace, it would be neater in my code structure.
>
> Probably there are no such way.

I suspect that is the case. But there is always the hope that I am
missing something!

> I only have functions with internal linkage in global namespace as code
> bases tend to be huge lately. Why you need it to be in global namespace?

It is in connection with interrupts and weak linkage (again, this isn't
the only way to handle this, but it would be convenient for my code
structure).

> I prefer to qualify explicitly instead of wrapping into namespace N {}.
> When it is something long like depths::of_various::spaces then I
> can shorten it with alias first:
>
> namespace N = depths::of_various::spaces;
>
> static int g(int x);
>
> int N::f(int x) { return x; }
>
> int g(int x) { return x; }
>
> int N::h(int x) { return x; }
>

That would be possible, I suppose, but there are quite a few internal
functions and a fair amount of internal data within the namespace.
These could themselves be put inside a singleton class, but a namespace
does that job just as well.


David Brown

unread,
Jun 14, 2021, 5:18:14 PM6/14/21
to
That's a neat suggestion - but it won't work in my particular case
because "g" should be a weak elf symbol. The reason it should be
outside the module's (in the old-fashioned sense of a cpp file with a
namespace - I am not yet using real C++ modules) namespace is that
another part of the code could override it by defining its own function
with that name. I don't want the overriding code to define the
overriding function inside the module's namespace.

At the moment, I am declaring "g" to be "extern C", which is a bit of a
sledgehammer solution but works.


David Brown

unread,
Jun 14, 2021, 5:19:14 PM6/14/21
to
I have, yes - I too thought it was the obvious solution!

Richard Damon

unread,
Jun 14, 2021, 8:48:39 PM6/14/21
to
On 6/14/21 4:26 PM, Louis Krupp wrote:
> On 6/14/2021 6:29 AM, David Brown wrote:
>> If I am inside a namespace, is there a way to declare a function as
>> being outside the namespace without closing and reopening it?  I.e., can
>> I do:
>>
>>
>> namespace N {
>>
>>     int f(int x) { return x; }
>>
>> }
>>
>> int g(int x) { return x; }
>>
>>
>> namespace N {
>>
>>     int h(int x) { return x; }
>>
>> }
>>
>> /without/ having to have "namespace N" twice?
>
> For what it's worth, the obvious attempt at a solution:
>
> ===
> namespace n
> {
>     void   nf1() {}
>     void ::gf2() {}
>     void   nf3() {}
> }
> ===
>
> doesn't work with g++:


but

namespace n
{
void nf1() {}
}
void gf2() {}

namespace n
{
void nf3() {}
}

Does.

Remember you can close and reopen namespaces unlike classes.

Nikolaj Lazic

unread,
Jun 14, 2021, 8:52:02 PM6/14/21
to
Dana Mon, 14 Jun 2021 20:48:30 -0400, Richard Damon <Ric...@Damon-Family.org> napis'o:
That was the exact thing OP did and now asks for the way to avoid it.

Richard Damon

unread,
Jun 14, 2021, 9:01:42 PM6/14/21
to
But if it IS right way to do it, it is the right way to do it.

The other option that I find messier but might work would be:

namespace n;

void n::hf1() {}

void gf2() {}

void n::nf3() {}


David Brown

unread,
Jun 15, 2021, 3:39:14 AM6/15/21
to
On 15/06/2021 02:51, Nikolaj Lazic wrote:
> Dana Mon, 14 Jun 2021 20:48:30 -0400, Richard Damon <Ric...@Damon-Family.org> napis'o:

>> namespace n
>> {
>> void nf1() {}
>> }
>> void gf2() {}
>>
>> namespace n
>> {
>> void nf3() {}
>> }
>>
>> Does.
>>
>> Remember you can close and reopen namespaces unlike classes.
>
> That was the exact thing OP did and now asks for the way to avoid it.
>

That was indeed one of the ways I know it can be done. I was hoping
there was a syntax that avoided that, which I had overlooked - basically
because I think it would have been neater in my code if something like
"void ::g() {}" had been allowed by the language. It looks like there
is no such syntax that I am missing. That's what I had expected, but it
was worth a try.

Thanks to all in the thread for their suggestions.
0 new messages