【问题标题】:MSVC and boost::lambda::bind error: T0: standard-argment not allowedMSVC 和 boost::lambda::bind 错误:T0:标准参数不允许
【发布时间】:2012-03-29 19:55:29
【问题描述】:

此代码可以使用 GCC 和 Clang 编译,但不能使用 MSVC 2010:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>
#include <list>
#include <string>

enum Actions {};
std::string eventStart(size_t index, Actions action, std::list<std::string> const& = std::list<std::string>()) { return ""; }

static void test() {
    using namespace boost::lambda;
    size_t i = 0;
    int action = 0;
    boost::function<std::string (std::list<std::string> const&)> startF = bind(eventStart, i, (Actions)action, _1);
}

给出错误:

1>c:\users\albert zeyer\documents\visual studio 2010\projects\test\test\test2.cpp(14): error C2383: 'T0': Für dieses Symbol sind Standardargumente nicht zulässig
1>          c:\users\albert zeyer\documents\visual studio 2010\projects\test\test\test2.cpp(14): Siehe Verweis auf die Instanziierung der gerade kompilierten Klassen-template "boost::lambda::detail::bind_tuple_mapper<T0,T1,T2,T3,T4,T5,T6,T7,T8,T9>".
1>          with
1>          [
1>              T0=std::string (size_t,Actions,const std::list<std::string> &),
1>              T1=const size_t,
1>              T2=const Actions,
1>              T3=boost::lambda::placeholder1_type,
1>              T4=boost::tuples::null_type,
1>              T5=boost::tuples::null_type,
1>              T6=boost::tuples::null_type,
1>              T7=boost::tuples::null_type,
1>              T8=boost::tuples::null_type,
1>              T9=boost::tuples::null_type
1>          ]
1>c:\users\albert zeyer\documents\visual studio 2010\projects\test\test\test2.cpp(14): error C2383: 'Arg1': Für dieses Symbol sind Standardargumente nicht zulässig

(题外话:我怎样才能得到英文的错误?)

使用&amp;eventStart 也不起作用,但错误不同:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxcallfun(7): error C2664: 'std::string (size_t,Actions,const std::list<_Ty> &)': Konvertierung des Parameters 3 von 'const boost::lambda::placeholder1_type' in 'const std::list<_Ty> &' nicht möglich
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          Ursache: Konvertierung von 'const boost::lambda::placeholder1_type' in 'const std::list<_Ty>' nicht möglich
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          Kein benutzerdefinierter Konvertierungsoperator verfügbar, der diese Konvertierung durchführen kann, oder der Operator kann nicht aufgerufen werden
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxbind1(292): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_Ret std::tr1::_Callable_fun<_Ty,_Indirect>::_ApplyX<_Ret,_Arg&,Actions&,boost::lambda::lambda_functor<T>&>(_Arg0,_Arg1,_Arg2) const".
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Ty=std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),
1>              _Indirect=false,
1>              _Arg=size_t,
1>              T=boost::lambda::placeholder<1>,
1>              _Arg0=size_t &,
1>              _Arg1=Actions &,
1>              _Arg2=const boost::lambda::placeholder1_type &
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxbind0(31): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_Ret std::tr1::_Bind3<_Callable,_Arg0,_Arg1,_Arg2>::_ApplyX<_Rx,const std::list<_Ty>&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Barg0,_Barg1,_Barg2,_Barg3,_Barg4,_Barg5,_Barg6,_Barg7,_Barg8,_Barg9)".
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Callable=std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,
1>              _Arg0=size_t,
1>              _Arg1=Actions,
1>              _Arg2=const boost::lambda::placeholder1_type,
1>              _Ty=std::string,
1>              _Barg0=const std::list<std::string> &,
1>              _Barg1=std::tr1::_Nil &,
1>              _Barg2=std::tr1::_Nil &,
1>              _Barg3=std::tr1::_Nil &,
1>              _Barg4=std::tr1::_Nil &,
1>              _Barg5=std::tr1::_Nil &,
1>              _Barg6=std::tr1::_Nil &,
1>              _Barg7=std::tr1::_Nil &,
1>              _Barg8=std::tr1::_Nil &,
1>              _Barg9=std::tr1::_Nil &
1>          ]
1>          c:\program files (x86)\boost\boost_1_47\boost\function\function_template.hpp(132): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "std::basic_string<_Elem,_Traits,_Ax> std::tr1::_Bind_base<_Ret,_BindN>::operator ()<const std::list<_Ty>&>(_Carg0)".
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>,
1>              _Ret=std::string,
1>              _BindN=std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>,
1>              _Ty=std::string,
1>              _Carg0=const std::list<std::string> &
1>          ]
1>          c:\program files (x86)\boost\boost_1_47\boost\function\function_template.hpp(126): Bei der Kompilierung der  Klassen-template der std::string boost::detail::function::function_obj_invoker1<FunctionObj,R,T0>::invoke(boost::detail::function::function_buffer &,T0)-Memberfunktion
1>          with
1>          [
1>              FunctionObj=std::tr1::_Bind<std::string,std::string,std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>>,
1>              R=std::string,
1>              T0=const std::list<std::string> 
1>          ]
1>          c:\program files (x86)\boost\boost_1_47\boost\function\function_template.hpp(907): Siehe Verweis auf die Instanziierung der gerade kompilierten Klassen-template "boost::detail::function::function_obj_invoker1<FunctionObj,R,T0>".
1>          with
1>          [
1>              FunctionObj=std::tr1::_Bind<std::string,std::string,std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>>,
1>              R=std::string,
1>              T0=const std::list<std::string> 
1>          ]
1>          c:\program files (x86)\boost\boost_1_47\boost\function\function_template.hpp(722): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "void boost::function1<R,T0>::assign_to<Functor>(Functor)".
1>          with
1>          [
1>              R=std::string,
1>              T0=const std::list<std::string> ,
1>              Functor=std::tr1::_Bind<std::string,std::string,std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>>
1>          ]
1>          c:\program files (x86)\boost\boost_1_47\boost\function\function_template.hpp(1043): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "boost::function1<R,T0>::function1<std::tr1::_Bind<_Result_type,_Ret,_BindN>>(Functor,int)".
1>          with
1>          [
1>              R=std::string,
1>              T0=const std::list<std::string> ,
1>              _Result_type=std::string,
1>              _Ret=std::string,
1>              _BindN=std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>,
1>              Functor=std::tr1::_Bind<std::string,std::string,std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>>
1>          ]
1>          c:\users\albert zeyer\documents\visual studio 2010\projects\test\test\test2.cpp(14): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "boost::function<Signature>::function<std::tr1::_Bind<_Result_type,_Ret,_BindN>>(Functor,int)".
1>          with
1>          [
1>              Signature=std::string (const std::list<std::string> &),
1>              _Result_type=std::string,
1>              _Ret=std::string,
1>              _BindN=std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>,
1>              Functor=std::tr1::_Bind<std::string,std::string,std::tr1::_Bind3<std::tr1::_Callable_fun<std::string (__cdecl *const )(size_t,Actions,const std::list<std::string> &),false>,size_t,Actions,const boost::lambda::placeholder1_type>>
1>          ]

为什么?代码似乎正确。

当我删除 eventStart 函数的默认参数时它可以工作。

【问题讨论】:

  • 一个简单的解决方法是使用bind(&amp;eventStart, ...) 而不是bind(eventStart, ...),但我不知道为什么这是必要的(或者在符合标准的编译器上是否有必要)。此外,对于新代码,您应该使用 Boost.Phoenix 而不是 Boost.Lambda,因为后者被认为已正式弃用,取而代之的是前者。
  • @ildjarn:也不起作用。我更新了问题。
  • 它适用于我,使用 VC++ 2010 SP1 和 Boost 1.49.0 进行测试。
  • @ildjarn:哪里说 Boost.Lambda 被弃用了?
  • 它还没有在官方文档中(因为 Boost.Lambda 维护者不存在并且无法更新它),但它已经在 Boost.Dev 邮件列表中发布了很多次。

标签: c++ visual-studio-2010 visual-c++ boost boost-lambda


【解决方案1】:

正如 ildjarn 建议的那样,我使用 &amp;eventStart 而不是 eventStart。仅此一项对我来说还不够。

但是当我将bind 更改为boost::lambda::bind 时,它可以工作。

这很奇怪,因为没有using namespace std; 左右。

【讨论】:

  • @MichaelBurr:嗯,很有趣。与我的问题完全相同的测试代码?并且还使用&amp;eventStart 而不是eventStart。您使用什么 MSVC 版本?
  • 如果我使用 both boost::lambda::bind&amp;eventStart 它会编译。我想我以前没有尝试过。我正在运行 cl.exe 版本为 16.00.40219.01 的 VS2010 SP1 (10.0.40219.1 SP1Rel)。
  • 谢谢,这对我也有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
相关资源
最近更新 更多