Boost logo

Boost :

From: Beman Dawes (bdawes_at_[hidden])
Date: 2002-01-21 14:29:04


At 04:41 AM 1/21/2002, Samuel Krempp wrote:

>On Mon, 2002-01-21 at 01:43, Beman Dawes wrote:
>> Is the only remaining objection the need for the constructor below to
>> be repeated for each possible number of arguments?
>
>well, personnally it is my only objection.
>Tom becker sample codes shows that the choice of this function call
>interface is independant from the rest of the library (just
>like the choice of operator% vs operator().. )
>
>> template<typename T1>
>> format(const char* s, T1 a1) : s_(s)
>> {
>> push_arg(&a1);
>> }
>
>'push_arg' can be replaced by 'operator%' and then
>the template constructors could be adapted over any operator-oriented
>format class to
>provide a function-call interface.
>
>We can provide both a function-call interface (with limited number of
>arguments),
>and the operator-call interface.
>
>the only inconvenience I see with this, is it will make yet another
>'two cases' documentation, eg :
>"""
>You can either call
>cout << format("%1 %2", x, y) ;
>
>or
>cout << format("%1 %2") % x % y;
>"""
>
>hum, this should not confuse the users much, so I think the function
>interface would be a good feature to add.

I disagree. There should only be one interface. Particularly in widely
used libraries like Boost (not to mention the standard library) it is very
confusing to some users to have multiple interfaces. It also increases the
specification and testing. Etc.

As to which interface, here is how I analyze the issue:

(1) If format has behavior virtually identical to printf, then providing
syntax which looks exactly like printf is reasonable and helpful. Thus
format("%1 %2", x, y) is the best choice.

(2) If format has behavior subtly different from printf, it is a disservice
to uses to use exactly the same syntax, because the syntax will mislead
them. Thus format("%1 %2") % x % y is better because it is evocative of
the behavior, without promising a compatibility with printf that it doesn't
deliver.

I should point out that Stan Lippman in 1993 or so used "behavior subtly
different from printf" as the rationale for rejecting a C++ Report article
I submitted using the format("%1 %2", x, y) interface. It was an article
on using nested function templates to simulate variable length argument
lists. I wasn't smart enough to recast the interface as format("%1 %2") %
x % y.

My guess is that format is closer to case (2) than case (1). Unless I'm
wrong about that, format("%1 %2") % x % y is preferable.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk