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.