Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a function from <cmath> with Boost.Lambda?
    primarykey
    data
    text
    <p>I try to transform all elements of a vector <code>v</code> into their log values with some other arithmetic operations (not in the code). How can I use Boost.Lambda to achieve that?</p> <p>As I say, there are some more arithmetic operations, so an expression with Boost.Bind doesn't work for me (too complicated, too long, unreadable).</p> <p>I don't want to use C++11 lambdas as well. But... would it change anything?</p> <p>My code is like:</p> <pre><code>#include &lt;boost/lambda/lambda.hpp&gt; #include &lt;cmath&gt; #include &lt;vector&gt; void testLambda() { using namespace boost::lambda; std::vector&lt;double&gt; v; v.push_back(1); v.push_back(2); v.push_back(3); std::transform(v.begin(), v.end(), v.begin(), _1 / 0.5); // works std::transform(v.begin(), v.end(), v.begin(), log(_1) / 0.5); // produces error //std::transform(v.begin(), v.end(), v.begin(), std::log(_1) / 0.5); // produces error //std::transform(v.begin(), v.end(), v.begin(), static_cast&lt;double(*)(double)&gt;(std::log)(_1) / 0.5); // produces error } </code></pre> <p>When I try to compile the code, MSVC2010 gives the error:</p> <pre><code>Error 1 error C2665: 'log' : none of the 3 overloads could convert all the argument types C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(120): could be 'double log(double)' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(527): or 'float log(float)' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(575): or 'long double log(long double)' while trying to match the argument list '(boost::lambda::placeholder1_type)' </code></pre> <p><strong>Update 1:</strong> I don't want to write functors for it, think that I would have to have a dozen of them, what then?</p> <p><strong>Update 2:</strong> I am able to do it with C++11 lambdas, but <strong>it's not what I ask for</strong>:</p> <pre><code> std::transform(v.begin(), v.end(), v.begin(), [](const double &amp; x) { return log(x) / 0.5; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload