问题描述
我在今天调试SWIG类型的电子书时遇到了一个有趣的问题。任何人都关心我为什么Visual C ++ 2008在转换从 myLib :: Char * 到时抛出转换失去限定符错误const ourLib :: Char *& ?我认为类型* - > const类型* 是一个平凡的转换, c> Lvalue - > Lvalue& 。
I ran into an interesting problem while debugging SWIG typemaps today. Anyone care to enlighten me why Visual C++ 2008 throws a "conversion loses qualifiers" error when converting from ourLib::Char * to const ourLib::Char * &? I thought Type * -> const Type * was a trivial conversion, and (when calling functions) Lvalue -> Lvalue & as well.
我们结束了:
// ourLib::Char is a typedef'ed char on Win32 %typemap(in) const char* (const ourLib::Char* tmp) { if (!bapiLua::LuaTraits<ourLib::Char*>::FromLuaObject(L, $argnum, tmp)) SWIG_fail; $1 = const_cast<char *>(tmp); } // And in a different source file, already written: namespace bapiLua { template<> struct LuaTraits<ourLib::Char*> { static ourLib::Bool FromLuaObject(lua_State* L, int pos, const ourLib::Char*& o_result); }; }
删除 const 从 const ourLib :: Char * tmp 导致我描述的错误。
Removing the const from const ourLib::Char * tmp causes the error I described.
推荐答案
p>说你有以下函数:
Say you had the following function:
void test( const char*& pRef) { static const char somedata[] = { 'a' ,'b', 'c', '\0'}; pRef = somedata; }
如果你传递一个非const ,那么当 test()返回时,编译器将丢失以下事实: p 指向的是 const 。
If you passed in a non-const char*, then when test() returned the compiler would have lost the fact that what p is pointing to is const.
这本质上与在这个C ++ FAQ Lite问题
It's essentially the same reason as given in this C++ FAQ Lite question (dealing with pointers-to-pointers rather than pointer references):
- http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17
这篇关于C ++“转换失去限定符”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!