|
Boost Users : |
From: Dave Dribin (dave-ml_at_[hidden])
Date: 2006-07-20 18:19:59
On Jul 20, 2006, at 4:40 PM, me22 wrote:
> I don't think this is a worry. varargs aren't typesafe and can
> certainly be avoided.
printf-like interfaces are still used quite a bit. The HFS+ code I
was writing was actually a device driver. The kernel debugging print
functions had printf-like interfaces, which is the only reason I know
this is an issue.
> 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.
Makes sense. I can live with .get(), and if it's consistent with
other libraries, then even better.
> 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.
The issue with using either cast is I have to remember the correct
type to use in the cast. By using a .get(), I don't have to worry
about it, and it's less repeated information. And as bonus, a
compiler that recognizes format strings will warn me if I use an
incorrect format string:
big8_t y = big8_t::init(5);
printf("%d\n", y.get());
gcc give's me this warning:
warning: format '%d' expects type 'int', but argument 2 has type
'int_least64_t'
If I just instinctively cast to int, I'd silently lose information.
The warning gives me a chance to use '%lld'.
> Much better to implement it like this:
> T get() const { return *this; }
> and not duplicate knowledge.
Agreed. Much better, thanks.
BTW, I started a thread on Boost-dev, as well, since that may be the
more appropriate list.
-Dave
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