|
Boost : |
From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2002-10-23 04:52:23
I think this is a good idea, but (IMHO) needs some polishing:
Having strict<T> derive from T renders this unusable for primitive
types. Instead, strict should hold some pointer to the object
passed and provide a conversion operator. Hopefully, all
the scaffolding vanishes at run-time after the optimizer does
its job.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Daniel Frey ha escrito:
> 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" );
>
> 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
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk