title: Compile errors using std::bind in gcc 4.7
body: <p>I am having a lot of trouble using <code>std::bind</code> in various places of my code. Sometimes it works, sometimes it doesn't, so I assume I am doing something fundamentally wrong. </p>
  
  <p>As far as I understand, the following basic use of <code>std::bind</code> should work fine:</p>
  
  <pre><code>#include &lt;functional&gt;
  
  int foo(int a, int b){ return a+b; }
  
  int main(){
  
      using namespace std::placeholders;
  
      // works
      auto bar_auto=std::bind(foo,1,_2);
  
      // compile error
      std::function&lt;int(int)&gt; bar_fun=std::bind(foo,1,_2);
  
      int quux=1;
      // compile error
      std::function&lt;int(int)&gt; bar_fun_lvalue=std::bind(foo,quux,_2);
  
  }
  </code></pre>
  
  <p>Surely the type of <code>bar_auto</code> is <code>std::function&lt;int(int)&gt;</code> (type of <code>foo</code> with 1 <code>int</code> argument bound), so why does <code>bar_fun</code> fail to compile? I included <code>bar_fun_lvalue</code> because some googling showed me that <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35569" rel="nofollow">rvalues used to be problematic</a>. but this didn't fix anything.</p>
  
  <p>It resembles <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52612" rel="nofollow">this bug</a>, but that's so old I don't expect it to be relevant. </p>
  
  <p>gcc's output is not particularly enlightening:</p>
  
  <blockquote>
    <p>In file included from bindnew.cpp:1:0:
    /usr/include/c++/4.7/functional: In instantiation of ‘static _Res
    std::_Function_handler&lt;_Res(_ArgTypes ...), _Functor>::_M_invoke(const
    std::_Any_data&amp;, _ArgTypes ...) [with _Res = int; _Functor =
    std::_Bind))(int, int)>; _ArgTypes =
    {int}]’: /usr/include/c++/4.7/functional:2298:6:   required from
    ‘std::function&lt;_Res(_ArgTypes ...)>::function(_Functor, typename
    std::enable_if&lt;(! std::is_integral&lt;_Functor>::value),
    std::function&lt;_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor =
    std::_Bind))(int, int)>; _Res = int;
    _ArgTypes = {int}; typename std::enable_if&lt;(! std::is_integral&lt;_Functor>::value), std::function&lt;_Res(_ArgTypes
    ...)>::_Useless>::type = std::function::_Useless]’
    bindnew.cpp:15:52:   required from here
    /usr/include/c++/4.7/functional:1912:40: error: no match for call to
    ‘(std::_Bind))(int, int)>) (int)’
    /usr/include/c++/4.7/functional:1140:11: note: candidates are:
    /usr/include/c++/4.7/functional:1211:2: note: template _Result std::_Bind&lt;_Functor(_Bound_args ...)>::operator()(_Args&amp;&amp; ...) [with _Args = {_Args ...}; _Result =
    _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder&lt;2>}] /usr/include/c++/4.7/functional:1211:2: note:<br>
    template argument deduction/substitution failed:
    /usr/include/c++/4.7/functional:1206:35: error: cannot convert
    ‘std::_No_tuple_element’ to ‘int’ in argument passing
    /usr/include/c++/4.7/functional:1225:2: note: template _Result std::_Bind&lt;_Functor(_Bound_args ...)>::operator()(_Args&amp;&amp; ...) const [with _Args = {_Args ...};
    _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder&lt;2>}] /usr/include/c++/4.7/functional:1225:2: note:<br>
    template argument deduction/substitution failed:
    /usr/include/c++/4.7/functional:1219:35: error: cannot convert
    ‘std::_No_tuple_element’ to ‘int’ in argument passing
    /usr/include/c++/4.7/functional:1239:2: note: template _Result std::_Bind&lt;_Functor(_Bound_args ...)>::operator()(_Args&amp;&amp; ...) volatile [with _Args = {_Args ...};
    _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder&lt;2>}] /usr/include/c++/4.7/functional:1239:2: note:<br>
    template argument deduction/substitution failed:
    /usr/include/c++/4.7/functional:1233:35: error: cannot convert
    ‘std::_No_tuple_element’ to ‘int’ in argument passing
    /usr/include/c++/4.7/functional:1253:2: note: template _Result std::_Bind&lt;_Functor(_Bound_args ...)>::operator()(_Args&amp;&amp; ...) const volatile [with _Args = {_Args
    ...}; _Result = _Result; _Functor = int (*)(int, int); _Bound_args =
    {int, std::_Placeholder&lt;2>}] /usr/include/c++/4.7/functional:1253:2:
    note:   template argument deduction/substitution failed:
    /usr/include/c++/4.7/functional:1247:35: error: cannot convert
    ‘std::_No_tuple_element’ to ‘int’ in argument passing</p>
  </blockquote>