Boost logo

Boost Users :

Subject: [Boost-users] [format] How to trigger compile-time error for mismatched arguments
From: Jeffrey Chang (jeffrey8chang_at_[hidden])
Date: 2008-11-11 00:48:43


Hi,

I'm using boost::format library, and this may be obvious to those
experienced users: is there any way to detect the mismatch of number of
expected arguments in a
boost::format object? Both of the following statements throw out runtime
exceptions (which is correct):
std::cout << boost::format ("%s %s") % "Hello";
std::cout << boost::format ("%s") % "Hello" % "world";

It'd be great if this type of parameter-mismatch error can be caught at
compile-time, instead of run-time. We use boost::format library extensively
for logging, and inevitably there're human errors like this mismatched
number of arguments. IMHO, this kind of error should have been detected by
compiler at build time, s.t. the developer won't be embarrassed by QA or
customers at run-time.

Actually, what's even worse is the following that doesn't generate error at
all:

std::cout << boost::format ("%s %s") % "Hello" % 12345;

There's a type-mismatch ('char*' versus 'int') for the second argument, but
there's neither compile-time nor run-time error. The output is "Hello
12345", which may be correct, but it may not be what the programmer intended
to do.

Any way of turning those typo into compile-time error?

--- Jeffrey



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