|
Boost Users : |
Subject: Re: [Boost-users] Fwd: Describing "traits" of a class's members
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2013-02-27 20:13:23
AMDG
On 02/27/2013 04:08 PM, Chris Stankevitz wrote:
>
> Can you modify the attached program so that the object
> TCPositionTraits holds all the traits for the TCPosition object. Not
> just the traits for the "double" parameters, as is currently the case.
> Notably missing from TCPositionTraits is a reference to
> TCPosition::mSteps.
>
That's actually pretty easy:
change TCDoubleTraits, TCUnsignedTraits, etc. to
template<class T>
struct TCTraits;
template<>
struct TCTraits<double>;
template<>
struct TCTraits<unsigned>;
change
struct TSItem { ... };
to
template<class T>
struct TSItemImpl
{
const TCTraits<T> *mTraits;
boost::function<const T&(const TCPosition*)> mGet;
};
typedef boost::variant<
TSItemImpl<double>,
TSItemImpl<unsigned> > TSItem;
then use boost::apply_visitor for all
operations on TSItems.
> Difficulty:
>
> This is a difficult question to answer due to the fact that
> TCPositionTraits has hard-coded in it the keyword "double". I cannot
> figure out how to remove this.
>
> Bonus:
>
> Bonus points if your answer looks something like "yes it is possible
> if you start using a boost concept which you apparently are not aware
> of".
>
> Note:
>
> You'll need the C++11 brace-initialization capability to compile this.
> I used "g++ -std=c++0x test.cpp" with gcc 4.6.3.
>
In Christ,
Steven Watanabe
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