On 7/20/06, Dave Dribin
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