Boost logo

Boost Users :

Subject: Re: [Boost-users] Declaring tricky templated variables such as disjoint_sets.
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2013-06-25 15:09:03


> this is a somewhat general C++ question, but I'm specifically concerned > about a Boost class in this case. > > How do I declare a boost::disjoint_sets<Rank, Parent, FindCompress> > variable without some kind of automatic template deduction? Usually I > see such classes declared taking their template parameters from within > a function such as: > > template <typename Rank, typename Parent> > void foo(Rank rank, Parent parent) > { > disjoint_sets<Rank, Parent> dset; > } > > but what if I want to declare dset at file scope? I've been trying to > manually construct the Rank and Parent types, but it's slightly doing > my head in and the iterator_property_map example seems to be stale. > > Is there a general C++ technique or a Boost-specific one that can help > me out here? Or is it just a matter of laboriously building up the > typedefs? Thanks, cheers. If you're using C++11, you can express the type of an expression 'expr' as 'decltype(expr)'. So, for example, you can write down the type of disjoint set that would be declared by the call foo(some_rank_expression, some_parent_expression) as disjoint_set<decltype(some_rank_expression), decltype(some_parent_expression)> At file scope (but not class scope), you can also use 'auto' like so: auto ds = make_disjoint_set(some_rank_expression, some_parent_expression); where 'make_disjoint_set' is a function that you would write that's similar to your 'foo', but would return the disjoint set. If you're using C++03, you can use Boost's emulations of 'decltype' and 'auto', which are BOOST_TYPEOF() and BOOST_AUTO() (see [1]), respectively. I'm not sure how well these work. Otherwise, you should be able to express the types more directly (albeit more verbosely) using metafunctions provided by the library that defines the types. I'm not familiar with the Disjoint Sets library, but if you provide a concrete example of rank and parent expressions, I can try to help you work through it. Hope that helps. Regards, Nate [1] http://www.boost.org/doc/libs/1_53_0/doc/html/typeof.html


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net