1

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.

EDIT: The solution we ended up going with:

// 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);
};
}

Removing the const from const ourLib::Char * tmp causes the error I described.

2
  • Seems like it shouldn't be an error from what you describe. Can you post some code that reproduces the error? Also, are there any other qualifiers involved other than const (e.g. volatile)? Aug 10, 2009 at 21:11
  • Is that suppose to be a lower-case char? Maybe you should post a bit of code.
    – GManNickG
    Aug 10, 2009 at 21:11

2 Answers 2

9

Say you had the following function:

void test(  const char*& pRef)
{
    static const char somedata[] = { 'a' ,'b', 'c', '\0'};
    pRef = somedata;
}

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.

It's essentially the same reason as given in this C++ FAQ Lite question (dealing with pointers-to-pointers rather than pointer references):

1
  • That would explain it. Argumentum ad Parashift has always been a convincing tactic.
    – ChrisV
    Aug 10, 2009 at 21:38
0

In following code,

friend ostream & operator<<(ostream & output, const List& other)
{
    for(int i=0;i<other.length();i++)
        output << other.getData()[i] << " ";

    return output;
}

to remove compilation error "Conversion loses qualifiers", for parameter "const List& other", I changed both following called methods to const.

T* getData() const
{
    return data;
}

int length() const
{
    return lSize;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.