I've found many uses for lazy evaluation, such as a tool for optimization (e.g. matrices).
Another use is syntactic sugar. But before I go overboard and make my code look at lot cleaner at the cost of runtime overhead, do compilers know how to optimize this kind of stuff? Or should I just only use it when the potential overhead is faster than not using lazy evaluation?
Following is an example of what I mean. It's not my actual use case, just a simple version of non-lazy vs lazy eval.
// original
template < class out,
class in >
out && lazy_cast( in && i )
{
return ( out && )( i );
}
// usage
char c1 = 10;
int c2 = lazy_cast< int >( c1 );
// lazy
template < class in >
class C_lazy_cast
{
public:
in && i;
template < class out && >
operator out &&( )
{
return ( out && )( i );
}
};
template < class in >
C_lazy_cast< in > lazy_cast( in && i )
{
return { std::forward< in >( i ) };
}
// usage
char c1 = 10;
int c2 = lazy_cast( c1 );
For the sake of completeness, information about MSVC, GCC, and clang should be enough.
Copyright License:
Author:「j__」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/63590336/is-lazy-evaluation-efficient-optimizable