Boost logo

Boost Users :

From: me22 (me22.ca_at_[hidden])
Date: 2006-07-20 17:40:15


On 7/20/06, Dave Dribin <dave-ml_at_[hidden]> wrote:
> One more area where they can't act like int's is in variable length
> arguments. Even using my init() method, the following code won't
> compile on the printf:
> [snip code]
>
I don't think this is a worry. varargs aren't typesafe and can
certainly be avoided.

> One way around this is to add a get() method, so you can use:
>
> printf("%d\n", x.get());
>
> Or, as I prefer, use operator():
>
> printf("%d\n", x());
>
The .get is much better. operator() is for calling functions, while
.get is already used in many places (smart pointers, for example) for
that purpose.

However, neither is needed. (int)x or boost::implicit_cast<int>(x)
will both work fine. Of course, this is assuming that the value in x
fits properly in an int. Better would be typeof_x::value_type, but
then you can't be sure that your format string matches, which is the
same point as varargs being evil in the first place.

> Implemented as follows:
> [snip code]
>
Much better to implement it like this:
         T get() const { return *this; }
and not duplicate knowledge.

~ Scott McMurray


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