【问题标题】:Luabind Function using std::string& Reference with pure_out_value policy not possible?Luabind 函数使用 std::string& 引用和 pure_out_value 策略不可能?
【发布时间】:2010-12-08 12:51:03
【问题描述】:

我正在尝试从函数返回一个字符串,但它无法编译。 当我用 int& 替换 std::string& 类型时,它会编译,但是我想另外返回布尔值 std::string 我该怎么做?

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))
];

在 lua 中:

success, content = __getElementContent("a", true)

我正在使用 luabind 0.8.1、最新的 Lua、boost 1.4、VC++ 2008

我收到以下编译器错误(似乎无法确定 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)

【问题讨论】:

标签: c++ stl boost lua luabind


【解决方案1】:

我逐字复制了您的代码,添加了#includes,为它创建了一个main() 和一个lua_State,它几乎可以编译。然后我只是删除了luabind::def 行末尾的逗号,并编译了它。我不知道逗号是否对您来说是错误的,但请尝试我在下面的内容并告诉我们。

注意:在 Linux 上使用 g++ -c file.cpp、GCC 4.2.4、Luabind 0.7 和 0.9(预发行版)编译。

#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))
    ];
}

【讨论】:

  • 不,逗号不是问题 您的代码存在同样的问题,似乎是 Visual Studio 编译器错误或 boost 或 luabind 8.1 您使用的是哪个 boost 版本?
  • 使用 vc++10 beta 2 没有帮助。
  • 也无法使用 boost 1.32 和 luabind 0.7,所以看起来这确实是 luabind 创建者不知道/测试过的 vc++ 编译器限制。我目前的解决方法是返回一个 luabind::object (包含所有返回值的表),然后在包装 lua 函数中使用解包(解包表)......不是很好。
  • 我使用的是 Boost 1.38。 VC++ 太糟糕了……也许可以尝试给 Luabind 维护者发电子邮件——他确实在努力。
【解决方案2】:

不是pure_out_value(_2),而是修改'pure_out_value(_3)'?

  luabind::def("__getElementContent", &luacw_getElementContent, luabind::pure_out_value(_3))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多