Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Idle curiosity re. using directive/declaration scoped to a givenclass - is this technique sensible?

Reply
Thread Tools

Re: Idle curiosity re. using directive/declaration scoped to a givenclass - is this technique sensible?

 
 
Ian Collins
Guest
Posts: n/a
 
      04-18-2010
On 04/19/10 10:35 AM, Stuart Golodetz wrote:
> Hi guys,
>
> Just curious what you think of the technique described below. It seems
> to solve an issue I've occasionally encountered, but it involves putting
> a using directive/declaration in a header file (but within a
> class-specific namespace). Just wondering whether this would be "frowned
> upon" or whether you think it's a valid technique please.
>
> The problem:
>
> I occasionally find I want to bring things from a namespace into scope
> within a class definition in a header file. AFAIK I can't do that in a
> trivial way, however. (Obviously I don't want to put the using
> directive/declaration somewhere where it will pollute the global
> namespace, or the actual namespace in which the class should reside.)
>
> A suggested solution:
>
> Put a class-specific namespace around the class and shove the using
> directive/declaration in that, e.g.
>
> #include <boost/shared_ptr.hpp>
>
> namespace N
> {
>
> namespace N__X
> {
> using namespace boost;
> struct X
> {
> shared_ptr<int> p;
> };
> }
>
> using N__X::X; // bring N__X::X into N (as originally intended)
>
> }
>
> (a) Does this seem reasonable?
> (b) Is there a better way at all?
>
> N.B. Using shared_ptr doesn't entirely illustrate why I find the
> technique appealing -- for a better example, consider something like a
> Boost Multi-Index Container.


Could you achieve the same result using one or more typedefs?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      04-19-2010
On 19 apr, 03:31, Stuart Golodetz <(E-Mail Removed)> wrote:
> On 18/04/2010 23:56, Ian Collins wrote:
>
>
>
> > On 04/19/10 10:35 AM, Stuart Golodetz wrote:
> >> Hi guys,

>
> >> Just curious what you think of the technique described below. It seems
> >> to solve an issue I've occasionally encountered, but it involves putting
> >> a using directive/declaration in a header file (but within a
> >> class-specific namespace). Just wondering whether this would be "frowned
> >> upon" or whether you think it's a valid technique please.

>
> >> The problem:

>
> >> I occasionally find I want to bring things from a namespace into scope
> >> within a class definition in a header file. AFAIK I can't do that in a
> >> trivial way, however. (Obviously I don't want to put the using
> >> directive/declaration somewhere where it will pollute the global
> >> namespace, or the actual namespace in which the class should reside.)

>
> >> A suggested solution:

>
> >> Put a class-specific namespace around the class and shove the using
> >> directive/declaration in that, e.g.

>
> >> #include <boost/shared_ptr.hpp>

>
> >> namespace N
> >> {

>
> >> namespace N__X
> >> {
> >> using namespace boost;
> >> struct X
> >> {
> >> shared_ptr<int> p;
> >> };
> >> }

>
> >> using N__X::X; // bring N__X::X into N (as originally intended)

>
> >> }

>
> >> (a) Does this seem reasonable?
> >> (b) Is there a better way at all?

>
> >> N.B. Using shared_ptr doesn't entirely illustrate why I find the
> >> technique appealing -- for a better example, consider something like a
> >> Boost Multi-Index Container.

>
> > Could you achieve the same result using one or more typedefs?

>
> Yes -- that's what I do if it's just for one or two things actually.
>
> The sort of use cases I was pondering are:
>
> (a) Things like Boost Multi-Index Container which can require quite a
> lot of typedefs -- which sort of obscure the code a bit.
>
> (b) Bringing a class template rather than an instance of it into scope
> -- with typedefs, for instance, I might have to do:
>
> typedef boost::shared_ptr<double> double_Ptr;
> typedef boost::shared_ptr<int> int_Ptr;
> typedef boost::shared_ptr<X> X_Ptr;
> //etc.
>
> I guess I was just wondering whether this would qualify as a respectable
> practice or whether it's just a hack


Typedefing has other goals. You put things into context. With
"shared_ptr<int>" to "int_Ptr" it is just renaming. It is worth to
typedef things that you are going to use:

typedef boost::adjacency_list< boost::vecS
, boost::vecS
, boost::undirectedS
, boost:roperty< boost::vertex_color_t,
int >
, boost:roperty< boost::edge_weight_t,
int >
, boost::no_property
, boost::listS
> MovementGraph;


I got probably sick of typing "boost" here, but this MovementGraph is
exposed in interface. For someone unfamiliar with Boost.Graph who is
reading it it is perhaps even worse Chinese if i remove all boost::
from it. At least now he rests assured that the things used are
probably described together with boost::adjacency_list somewhere and
not my own makings.
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 2:29*pm, Stuart Golodetz <(E-Mail Removed)> wrote:
> On 19/04/2010 02:09, Öö Tiib wrote:
> > typedef boost::adjacency_list< *boost::vecS
> > * * * * * * * * * * * * * * * , boost::vecS
> > * * * * * * * * * * * * * * * , boost::undirectedS
> > * * * * * * * * * * * * * * * , boost:roperty< boost::vertex_color_t,
> > int>
> > * * * * * * * * * * * * * * * , boost:roperty< boost::edge_weight_t,
> > int>
> > * * * * * * * * * * * * * * * , boost::no_property
> > * * * * * * * * * * * * * * * , boost::listS
> > * * * * * * * * * * * * * * * > *MovementGraph;

>
> > I got probably sick of typing "boost" here, but this MovementGraph is
> > exposed in interface. For someone unfamiliar with Boost.Graph who is
> > reading it it is perhaps even worse Chinese if i remove all boost::
> > from it. At least now he rests assured that the things used are
> > probably described together with boost::adjacency_list somewhere and
> > not my own makings.

>
> It's a fair point. I have to say I usually get sick of typing "boost"
> very quickly with things like this though -- aside from the fact that
> it's exposed in interface (which I agree makes it debatable), this was
> exactly the sort of use case I had in mind.


Yes, writing intuitive public interfaces is difficult. I think it is
less about making it shorter to type and more about making it easier
to understand and to use. C++ has powerful ways to hide irrelevant
information. Often it is not needed to expose such constructs like
above in header file. When it is needed then i prefer raw forms for
things that i do not expect to be used. I did not typedef
boost:roperty<boost::vertex_color_t,int> to display that i do not
expect it to be used as separate type by users of that interface.
There may be reasons why i did not hide and enwrap that MovementGraph.
You may also have reasons not to hide usage of that multi-index-
container (behind Pimpl idiom for example). Such reasons are worth to
think about when it feels like typing too lot of boost:: in header.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Point of idle curiosity Chris Angelico Python 5 11-19-2012 08:54 PM
Re: Point of idle curiosity Terry Reedy Python 0 11-18-2012 07:27 PM
Idle Curiosity: no head Joy Beeson HTML 5 01-22-2011 06:59 AM
How to identify whether a function is module scoped function orstatic method of a class by using its fully qualified name srinivasan srinivas Python 0 07-02-2008 10:32 AM
Just out of curiosity: Which languages are they using at Google and what for? alainpoint@yahoo.fr Python 3 06-19-2006 03:18 PM



Advertisments