c++ - 使用带有 pure_out_value 策略的 std::string& Reference 的 Luabind 函数不可能吗?

标签 c++ stl boost lua luabind

我正在尝试从函数返回一个字符串,但它无法编译。 当我用 int& 替换 std::string& 类型时,它会编译,但是我想另外返回 bool 值 a 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)

最佳答案

我一字不差地复制了你的代码,添加了#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))
    ];
}

关于c++ - 使用带有 pure_out_value 策略的 std::string& Reference 的 Luabind 函数不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1597697/

相关文章:

c++ - 关于关于类的 vector

c++ - 如何使用标准 C++ 从 UTF-8 转换为 ANSI

c++ - boost::make_shared 底层对象和引用计数对象的内存分配大小在哪里?

c++ - C 预处理器扩展到另一个类似对象的宏

c++ - 如何处理与 C++ 中的 unicode 支持相关的代码重复

c++ - 在 Enter 键处拆​​分字符串

boost - 使用 MinGW 构建 Boost 1.52

c++ - 为什么 std::pair 暴露成员变量?

c++ - 有没有办法从 map 中获取特定的键值对?

c++ - 根据给定的概率分布从集合中提取值