Topic: expressions as nullary reference_closures


Author: Triple-DES <DenPlettfrie@gmail.com>
Date: Mon, 2 Feb 2009 09:47:39 CST
Raw View
On 7 Jan, 19:15, Jonas Persson <l.j.pers...@gmail.com> wrote:
> Hi all,
> I have been thinking about a small change for the new lambdas:
> Implicit conversion
> from an expression to a std::reference_closure<R()>. Have this already
> been
> considered?
> It would allow some nice things, like declaring shortcutting && and ||
> operators for user defined types, implementation of assert and logging
> where
> the arguments is only evaluated if used, without the use of macros.
>
> // only evaluates f is asserts is enabled
>  assert(std::reference_closure<bool()> f) {
>         if(isAssertEnabled)
>                 if(!f())
>                         attach_debugger_or_abort();
>  }
>
> // argument is only evaluated if asserts are enabled
> assert(isAllMyDataCorrect() && hasAllDataBeenWrittenToDatabase());
>
> // Solves short cutting &&
> // Only evaluateds rhs if lhs is true
> bool operator&&(MyBool lhs,std::reference_closure<MyBool()> rhs) {
>        if(!lhs.isTrue()) return false;
>        return rhs().isTrue();
>
> }
>
>  What do you think? useful, dangerous or just unnecessary?

I think it's a very interesting idea, but it's not really a "small
change". Consider:

extern String get_text( const std::reference_closure<int()>& expr );

int main()
{
   // _possibly_ calls operator<<(std::ostream&, const
std::reference_closure<String()>&),
   // in which case get_text is _possibly_ called, which may or may not
result in a call to rand()
   f( std::cout << get_text( rand() ) );
}

This makes it very hard to determine which parts of the code that are
actually evaluated without looking at every function's definition.
This is a huge change from today, where the arguments to a function
are always evaluated.

Of course, this is similar to the argument that operator overloading
makes code unreadable. Your proposal certainly has its uses.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jonas Persson <l.j.persson@gmail.com>
Date: Wed, 7 Jan 2009 12:15:01 CST
Raw View
Hi all,
I have been thinking about a small change for the new lambdas:
Implicit conversion
from an expression to a std::reference_closure<R()>. Have this already
been
considered?
It would allow some nice things, like declaring shortcutting && and ||
operators for user defined types, implementation of assert and logging
where
the arguments is only evaluated if used, without the use of macros.

// only evaluates f is asserts is enabled
 assert(std::reference_closure<bool()> f) {
        if(isAssertEnabled)
                if(!f())
                        attach_debugger_or_abort();
 }

// argument is only evaluated if asserts are enabled
assert(isAllMyDataCorrect() && hasAllDataBeenWrittenToDatabase());

// Solves short cutting &&
// Only evaluateds rhs if lhs is true
bool operator&&(MyBool lhs,std::reference_closure<MyBool()> rhs) {
       if(!lhs.isTrue()) return false;
       return rhs().isTrue();
}

 What do you think? useful, dangerous or just unnecessary?

 / Jonas

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]