1

私はこの関数をコンパイルしようとします:

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

void foo(unsigned n)
{
  std::vector<unsigned> some_vector;
  /* fill vector ... */
  auto le_sqrt_n = std::bind(std::less_equal<unsigned>, 
                             std::placeholders::_1, 
                             unsigned(sqrt(n))
                             );
  auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n);
  /* do something with the result ... */
}

gcc 4.6.3の場合:

g++ -std=c++0x test_bind_02.cc

このエラーが発生します:

test_bind_02.cc: In function ‘void foo(unsigned int)’:
test_bind_02.cc:10:55: error: expected primary-expression before ‘,’ token
test_bind_02.cc:13:9: error: unable to deduce ‘auto’ from ‘&lt;expression error>’
test_bind_02.cc:14:77: error: unable to deduce ‘auto’ from ‘&lt;expression error>’

私の(おそらく愚かな)間違いは何ですか?

4

1 に答える 1

10

あなたは電話しませんstd::less_equal<unsigned> c-tor。このコードは問題なく動作します。

http://liveworkspace.org/code/7f779d3fd6d521e8d4012a4066f2c40f

std::bind2つの形式があります。

template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );

構造体なので、完全に構築されたオブジェクトstd::less_equalとしてこの関数に渡す必要があります。F作品コードは

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

void foo(unsigned n)
{
  std::vector<unsigned> some_vector;
  /* fill vector */
  auto le_sqrt_n = std::bind(std::less_equal<unsigned>(),
                 std::placeholders::_1, 
                 unsigned(sqrt(n))
                 );
  auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n);
}
于 2012-08-14T09:00:41.373 に答える