Topic: inline using, rather than inline namespace?


Author: Colin <google@icemx.net>
Date: Sat, 10 Apr 2010 13:14:22 CST
Raw View
Hi All,

when a library defines a class template that is intended to be
specialised by the user [of the library], the specialisation must be
defined in the namespace of the library, not in the namespace of the
code using the library.

At first sight, I thought that inline namespace would solve this
problem, but that doesn't seem to be the case     what is needed here is
a kind of "inline using", which should make a name imported into a
namespace behave "as if" it were declared there in the first place,
and in particular allow the specialisation of templates:

namespace library
{
  template< typename T > struct foo
  {
     static void frobnicate() { /* default implementation that uses T
in some way */ }
  };
}

namespace application
{
  struct bar { /* ... */ };

  inline using library::foo;  // The "inline"...

  template<> struct foo< bar >  // ...enables the specialisation of
foo here in this namespace.
  {
     static void frobnicate() { /* cooler implementation for bar */ }
  }
}

Any thoughts on this?

Thanks, Colin


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Bo Persson" <bop@gmb.dk>
Date: Mon, 12 Apr 2010 12:07:18 CST
Raw View
Colin wrote:
> Hi All,
>
> when a library defines a class template that is intended to be
> specialised by the user [of the library], the specialisation must be
> defined in the namespace of the library, not in the namespace of the
> code using the library.
>
> At first sight, I thought that inline namespace would solve this
> problem, but that doesn't seem to be the case     what is needed here
> is a kind of "inline using", which should make a name imported into
> a namespace behave "as if" it were declared there in the first
> place, and in particular allow the specialisation of templates:
>
> namespace library
> {
>  template< typename T > struct foo
>  {
>     static void frobnicate() { /* default implementation that uses T
> in some way */ }
>  };
> }
>
> namespace application
> {
>  struct bar { /* ... */ };
>
>  inline using library::foo;  // The "inline"...
>
>  template<> struct foo< bar >  // ...enables the specialisation of
> foo here in this namespace.
>  {
>     static void frobnicate() { /* cooler implementation for bar */ }
>  }
> }
>
> Any thoughts on this?
>

Isn't the problem that it is the library that needs the
specialization? How does it know it should go looking for inline
usings everywhere else?


You can also run into the old pre-namespace name collisions:

inline using library1::foo;
inline using library2::foo;

template<> struct foo< bar >  // What are we specializing here?
{   };


Bo Persson



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?windows-1252?Q?Kri=9Atof_=8Eelechovski?= <giecrilj@stegny.2a.pl>
Date: Mon, 12 Apr 2010 12:06:50 CST
Raw View
Uzytkownik "Colin" <google@icemx.net> napisal w wiadomosci
news:db2a71e5-2588-48dd-ac9f-ab342d627c99@b23g2000yqn.googlegroups.com...
> Hi All,
>
> when a library defines a class template that is intended to be
> specialised by the user [of the library], the specialisation must be
> defined in the namespace of the library, not in the namespace of the
> code using the library.
>
> At first sight, I thought that inline namespace would solve this
> problem, but that doesn't seem to be the case     what is needed here is
> a kind of "inline using", which should make a name imported into a
> namespace behave "as if" it were declared there in the first place,
> and in particular allow the specialisation of templates:
>


You do not inject code in C++.  This is by design.  A strict policy
about what can be defined where helps to maintain code integrity (and
to reanimate legacy code).
Why do you think this is a problem in the first place?
Best regards,
Chris


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Colin <google@icemx.net>
Date: Mon, 12 Apr 2010 20:47:20 CST
Raw View
On Apr 12, 8:06 pm, Kri=B9tof =AEelechovski <giecr...@stegny.2a.pl> wrote:
> Uzytkownik "Colin" <goo...@icemx.net> napisal w
wiadomoscinews:db2a71e5-2588-48dd-ac9f-ab342d627c99@b23g2000yqn.googlegroup=
s.com.
..
>
> > when a library defines a class template that is intended to be
> > specialised by the user [of the library], the specialisation must be
> > defined in the namespace of the library, not in the namespace of the
> > code using the library.
>
> > At first sight, I thought that inline namespace would solve this
> > problem, but that doesn't seem to be the case -- what is needed here is
> > a kind of "inline using", which should make a name imported into a
> > namespace behave "as if" it were declared there in the first place,
> > and in particular allow the specialisation of templates:
>
> You do not inject code in C++.  This is by design.  A strict policy
> about what can be defined where helps to maintain code integrity (and
> to reanimate legacy code).
> Why do you think this is a problem in the first place?

When writing an application, and I need to specialise a class template
[for one of my application classes] that's in a library [and therefore
a different namespace], you have to, in your code, "switch" from your
namespace into the library's namespace [where you then have to
qualify, or import via "using", all your stuff from your namespace
into that of the library].

Perhaps this problem is not as wide-spread as I might assume from my
own code? Do other people not see this "switching" of namespaces as
awkward? ...?

Regards, Colin


PS: A straightforward alternative would be to allow namespace
qualifications in declarations, definitions and specialisations (also
function definitions etc), e.g.:

namespace foo
{
  struct A {};

  template<> struct ::lib::bar< A > { /* Stey in ::foo here */ };  //
Put the specialisation of bar into namespace ::lib, rather than ::foo.
};


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]