Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-10-23 04:17:46

Hi Boosters,

I always wanted to have "explicit parameters". Let me first explain what
I understand by explicit parameters: Today, you have a function like

void f( const std::string& s ) { ... }

where you can call it with a string or with something which can be used
to create a temporary string:

f( std::string( "hello" ) );
f( "world" );

When you start overloading functions, you sometimes want to restrict
your function to accept no temporaries. You want the first call above to
succeed and the second to fail. An easy solution is:

void f( std::string& s ) { ... }

but of course the interface is "lying" now. Given all the "common
knowledge", users would expect the function to modify s. This is where I
had a very simple idea which might solve the problem. To the caller, it
looks like T&, to the function, it looks like const T&. Consider:

template< typename T > struct strict : T {
  strict( T& t ) : T( t ) {}

Now all you need to do is:

void f( const strict< std::string >& s ) { ... }

and it works as I would expect it. What do you think? Is this a
candidate for utility.hpp?

Regards, Daniel

Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web:

Boost list run by bdawes at, gregod at, cpdaniel at, john at