What is "global using-declaration" in N2378?

96 views
Skip to first unread message

Andrzej Krzemieński

unread,
Oct 26, 2012, 6:49:02 AM10/26/12
to std-dis...@isocpp.org
Hi,
I am reading paper N2378 (User-defined Literals - revision 3). In section 3.5 it recommends an idiom for defining literal operators in separate namespaces. But just before that it lists different approaches to namespace choices that it considers inferior:

"It is tempting therefore to declare user-defined literal operators in the global namespace, or to place a global using-declaration in the global namespace. Unfortunately, that is a recipe for declaration conflicts even for code that might not use the literal forms."

What does it mean to "place a global using-declaration in the global namespace"? The only situation that comes to my mind is this:

// File a.h:
namespace A {
  class X {};
  namespace literals {
    void operator "" _X(char const*);
  }
}
using A::literals::operator "" _X;

// File b.h:
namespace B {
  class ExtNum {};
  namespace literals {
    void operator "" _X(char const*);
  }
}
using B::literals::operator "" _X;

// File main.c:
#include "a.h"
#include "b.h" // No conflict.

But I fail to see how this would be "a recipe for declaration conflicts even for code that might not use the literal forms".
Can anyone help me understand?

Regards,
&rzej

Daniel Krügler

unread,
Oct 26, 2012, 8:12:54 AM10/26/12
to std-dis...@isocpp.org
2012/10/26 Andrzej Krzemieński <akrz...@gmail.com>:
> I am reading paper N2378 (User-defined Literals - revision 3). In section
> 3.5 it recommends an idiom for defining literal operators in separate
> namespaces. But just before that it lists different approaches to namespace
> choices that it considers inferior:
>
> "It is tempting therefore to declare user-defined literal operators in the
> global namespace, or to place a global using-declaration in the global
> namespace. Unfortunately, that is a recipe for declaration conflicts even
> for code that might not use the literal forms."
>
> What does it mean to "place a global using-declaration in the global
> namespace"? The only situation that comes to my mind is this:
>
> // File a.h:
> namespace A {
> class X {};
> namespace literals {
> void operator "" _X(char const*);
> }
> }
> using A::literals::operator "" _X;

Yes, I believe that this is meant here as "global using-declaration in
the global namespace".

> // File b.h:
> namespace B {
> class ExtNum {};
> namespace literals {
> void operator "" _X(char const*);
> }
> }
> using B::literals::operator "" _X;
>
>
> // File main.c:
> #include "a.h"
> #include "b.h" // No conflict.

This is correct, but there is a still a difference when a third declaration for
the same name and parameter types exists in the global namespace. E.g.
given above declarations, the following additional declaration
(possibly unintended
included by either a.h or b.h):

whatever operator "" _X(char const*);

will be ill-formed. This problem does not occur via a using-directive.

I do not know whether this was meant here, but it would make sense.

- Daniel

ajay kumar

unread,
Oct 4, 2015, 7:13:41 AM10/4/15
to ISO C++ Standard - Discussion
Reply all
Reply to author
Forward
0 new messages