Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost::format] operator % could use call_traits<..>::parameter_type
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2012-06-25 05:22:57


2012/6/25 Luc Bourhis <luc_j_bourhis_at_[hidden]>

> Hi,
>
> consider a library featuring code like
>
> template <..., unsigned Rank, ...> struct Foo { ... static unsigned const
> rank = Rank; .... };
>
> as well as
>
> boost::format(...) % Foo::rank
>
> Such code will link if and only if a definition for Foo::rank is provided
> because the interface of operator % is
>
> template <class T>
> operator%(const T& x)
>
> This is not possible in the library, as I do not know which values of Rank
> will be used by client code. Thus the only possibility is a hat trick like
>
> boost::format(...) % (Foo::rank+0)
>

Hi,

For problems like this I use a trick a bit simpler:
boost::format(...) % +Foo::rank

Notice, that if foo looks uses some char type instead of int, like this:
typedef int8_t-or-char MyInt;
template <..., MyInt Rank, ...> struct Foo { ... static MyInt const rank =
Rank; .... };
then both our above tricks change the behavior of format by promoting Rank
to int, which is treated totally different than char. In my case the "char"
behavior was unexpected, and the trick fixed that as well ;-)

My 0.02 cents.
Regards
Kris



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