Luabind Function using std::string& Reference with pure_out_value policy not possible?
I'am trying to return a string from a function but it doesn't compile. When I replace the std::string& type with int& it compiles, however I want to return additionally to the boolean a std::string how do I do this?
bool luacw_getElementContent( std::string name, std::string& content, bool fromBeginning )
{
content = "test";
return false;
}
...
luabind::module(myLuaState) [
luabind::def("__getElementContent", &luacw_getElementContent, luabind::pure_out_value(_2))
];
in lua:
success, content = __getElementContent("a", true)
I'm using luabind 0.8.1, most recent Lua, boost 1.4, VC++ 2008
I get the following compiler error (it seems it can't determine a size for std::string& ?
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2784: 'luabind::detail::char_array<开发者_开发技巧;sizeof(identity<T>::type)> luabind::detail::indirect_sizeof_test(luabind::detail::by_value<T>)' : could not deduce template argument for 'luabind::detail::by_value<T>' from 'luabind::detail::by_reference<T>'
1> with
1> [
1> T=std::string
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(71) : see declaration of 'luabind::detail::indirect_sizeof_test'
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(228) : see reference to class template instantiation 'luabind::detail::indirect_sizeof<T>' being compiled
1> with
1> [
1> T=a1
1> ]
1> d:\osxdata\libs\boost\src\boost\mpl\aux_\preprocessed\plain\apply_wrap.hpp(49) : see reference to class template instantiation 'luabind::detail::pure_out_value_policy<BOOST_PP_ITERATION_0,Policies>::apply<T,Direction>' being compiled
1> with
1> [
1> BOOST_PP_ITERATION_0=2,
1> Policies=luabind::detail::null_type,
1> T=a1 ,
1> Direction=luabind::detail::lua_to_cpp
1> ]
1> d:\osxdata\libs\boost\src\boost\preprocessor\iteration\detail\local.hpp(37) : see reference to class template instantiation 'boost::mpl::apply_wrap2<F,T1,T2>' being compiled
1> with
1> [
1> F=p1,
1> T1=a1,
1> T2=luabind::detail::lua_to_cpp
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\detail\call.hpp(48) : see reference to function template instantiation 'int luabind::detail::invoke_normal<bool(__cdecl *)(std::string,std::string &,bool),boost::mpl::vector4<T0,T1,T2,T3>,Policies>(lua_State *,const F &,Signature,const Policies &,boost::mpl::long_<N>,boost::mpl::false_)' being compiled
1> with
1> [
1> T0=bool,
1> T1=std::string,
1> T2=std::string &,
1> T3=bool,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>,
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>,
1> N=3
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\detail\call.hpp(58) : see reference to function template instantiation 'int luabind::detail::invoke0<bool(__cdecl *)(std::string,std::string &,bool),boost::mpl::vector4<T0,T1,T2,T3>,Policies,boost::is_void<T>>(lua_State *,const F &,Signature,const Policies &,IsVoid,boost::mpl::false_)' being compiled
1> with
1> [
1> T0=bool,
1> T1=std::string,
1> T2=std::string &,
1> T3=bool,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>,
1> T=bool,
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>,
1> IsVoid=boost::is_void<bool>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\make_function.hpp(31) : see reference to function template instantiation 'int luabind::detail::invoke<bool(__cdecl *)(std::string,std::string &,bool),boost::mpl::vector4<T0,T1,T2,T3>,luabind::detail::policy_cons<H,T>>(lua_State *,const F &,Signature,const Policies &)' being compiled
1> with
1> [
1> T0=bool,
1> T1=std::string,
1> T2=std::string &,
1> T3=bool,
1> H=luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,
1> T=luabind::detail::null_type,
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\make_function.hpp(30) : while compiling class template member function 'int luabind::detail::function_invoke<F,Signature,Policies>::operator ()(lua_State *) const'
1> with
1> [
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\make_function.hpp(76) : see reference to class template instantiation 'luabind::detail::function_invoke<F,Signature,Policies>' being compiled
1> with
1> [
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\function.hpp(28) : see reference to function template instantiation 'luabind::adl::object luabind::make_function<bool(__cdecl *)(std::string,std::string &,bool),boost::mpl::vector4<T0,T1,T2,T3>,Policies>(lua_State *,F,Signature,Policies)' being compiled
1> with
1> [
1> T0=bool,
1> T1=std::string,
1> T2=std::string &,
1> T3=bool,
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>,
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Signature=boost::mpl::vector4<bool,std::string,std::string &,bool>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\function.hpp(27) : while compiling class template member function 'void luabind::detail::function_registration<F,Policies>::register_(lua_State *) const'
1> with
1> [
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\function.hpp(50) : see reference to class template instantiation 'luabind::detail::function_registration<F,Policies>' being compiled
1> with
1> [
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1> d:\osxdata\libs\scintillawrapper\mainfrm.cpp(191) : see reference to function template instantiation 'luabind::scope luabind::def<bool(__cdecl *)(std::string,std::string &,bool),luabind::detail::policy_cons<H,T>>(const char *,F,const Policies &)' being compiled
1> with
1> [
1> H=luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,
1> T=luabind::detail::null_type,
1> F=bool (__cdecl *)(std::string,std::string &,bool),
1> Policies=luabind::detail::policy_cons<luabind::detail::pure_out_value_policy<2,luabind::detail::null_type>,luabind::detail::null_type>
1> ]
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2784: 'luabind::detail::char_array<sizeof(identity<T>::type)> luabind::detail::indirect_sizeof_test(luabind::detail::by_const_pointer<T>)' : could not deduce template argument for 'luabind::detail::by_const_pointer<T>' from 'luabind::detail::by_reference<T>'
1> with
1> [
1> T=std::string
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(68) : see declaration of 'luabind::detail::indirect_sizeof_test'
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2784: 'luabind::detail::char_array<sizeof(identity<T>::type)> luabind::detail::indirect_sizeof_test(luabind::detail::by_pointer<T>)' : could not deduce template argument for 'luabind::detail::by_pointer<T>' from 'luabind::detail::by_reference<T>'
1> with
1> [
1> T=std::string
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(65) : see declaration of 'luabind::detail::indirect_sizeof_test'
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2784: 'luabind::detail::char_array<sizeof(identity<T>::type)> luabind::detail::indirect_sizeof_test(luabind::detail::by_const_reference<T>)' : could not deduce template argument for 'luabind::detail::by_const_reference<T>' from 'luabind::detail::by_reference<T>'
1> with
1> [
1> T=std::string
1> ]
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(62) : see declaration of 'luabind::detail::indirect_sizeof_test'
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2893: Failed to specialize function template 'luabind::detail::char_array<sizeof(identity<T>::type)> luabind::detail::indirect_sizeof_test(luabind::detail::by_reference<T>)'
1> With the following template arguments:
1> 'std::string'
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(78) : error C2866: 'luabind::detail::indirect_sizeof<T>::value' : a const static data member of a managed type must be initialized at the point of declaration
1> with
1> [
1> T=a1
1> ]
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(228) : error C2975: 'Size' : invalid template argument for 'luabind::detail::pure_out_value_converter', expected compile-time constant expression
1> d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(163) : see declaration of 'Size'
1>d:\osxdata\libs\luabind\luabind-0.8.1\luabind\out_value_policy.hpp(211) : warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1> d:\osxdata\libs\boost\src\boost\preprocessor\iteration\detail\local.hpp(37) : see reference to class template instantiation 'luabind::detail::pure_out_value_converter<Size,Policies>' being compiled
1> with
1> [
1> Size=0,
1> Policies=luabind::detail::null_type
1> ]
1>d:\osxdata\libs\boost\src\boost\preprocessor\iteration\detail\local.hpp(37) : warning C4815: 'c1' : zero-sized array in stack object will have no elements (unless the object is an aggregate that has been aggregate initialized)
I copied your code verbatim, added the #includes
, made a main()
for it and a lua_State
, and it almost compiled. Then I just removed the comma at the end of the luabind::def
line, and it compiled. I don't know if the comma is all that was wrong for you, but try what I have below and let us know.
Note: compiled with g++ -c file.cpp
, GCC 4.2.4, Luabind 0.7 and also 0.9 (pre-release), on Linux.
#include <luabind/luabind.hpp>
#include <luabind/out_value_policy.hpp>`
bool luacw_getElementContent( std::string name, std::string& content, bool fromBeginning )
{
content = "test";
return false;
}
int main()
{
lua_State* myLuaState = NULL;
luabind::module(myLuaState)
[
luabind::def("__getElementContent", &luacw_getElementContent, luabind::pure_out_value(_2))
];
}
Instead of pure_out_value(_2), modify 'pure_out_value(_3)'?
luabind::def("__getElementContent", &luacw_getElementContent, luabind::pure_out_value(_3))
精彩评论