Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-10-23 05:03:19


Daniel Frey wrote:
>
> 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
> this:
>
> 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" );

Forgive my stupidity. Of course I meant:

std::string s = "hello";
f( s ); // Should work

f( std::string( "funny" ) ); // Shouldn't work
f( "world" ); // Shouldn't work

As the later two are temporaries, while the first is not :)

> 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: http://www.aixigo.de

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