Q:

C++ "conversion loses qualifiers" compile error

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.

A:

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):

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

