Boost logo

Boost :

Subject: Re: [boost] Discovering C++11 lambda argument and result types to construct boost::function
From: Frank Birbacher (bloodymir.crap_at_[hidden])
Date: 2012-02-11 13:45:14


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Am 10.02.12 09:39, schrieb Andrzej Krzemienski:
> Sorry if this question is silly, but could you give me an example
> of where this deduction mechanism could be used?

The example I gave on comp.lang.c++.moderated is quite plain. The
make_function is used to construct a class template instatiation. I
think it compares to std::make_pair. I'll give another example below.

What I'm trying to work with is a lazy computation. My class template
Lazy<T> contains a variant over the type T and a function<T()> that
can produce a value of that type.

template<typename T>
struct Lazy<T>
{
  typedef T value_type;
  typedef function<value_type()> lazy_function_type;
  typedef variant<value_type, lazy_function_type> data_type;
  data_type data;

  //...ctors...

  value_type& get()
  {
    if(auto* const p = boost::get<lazy_function_type>(&data))
    {
      data = (*p)();
    }
    return boost::get<value_type>(data);
  }
};

The real task now is to create a make_lazy() function that constructs
a Lazy<T> from a lambda expression. Even when the target type is
somehow known there is still an advantage in convenience, esp. with
"auto var = make_lazy(...)." When calling template functions that take
Lazy<T> for some T the type deduction would not work with just a
lambda expression.

I hope I could show to you that there is some usecase for make_function.

Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: keyserver x-hkp://pool.sks-keyservers.net

iEYEARECAAYFAk82tzoACgkQhAOUmAZhnmrVYgCfUlN9FgIwgg0EqCbObBtNR2DR
x0cAn14TOlJh5JotT7DI4Kv22uCP9+UJ
=l6bv
-----END PGP SIGNATURE-----


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