c++qtcastingreferenceqdbus

How to resolve this "cast to pointer from type of different size" warning?


I have two programs that communicate over the D-Bus. In one of the there is a function that takes two parameters namely const char* data and int size and in another one I have a function that returns a value of the type unsigned char. I place a function call in the first program like this function((char*)program2->function().value(), 1) but i get the warning cast to pointer from integer of different size [-Wint-to-pointer-cast]. how should i resolve this warning? also I'm using Qt libraries.

EDIT:

actually in the function that takes the const char* and int I append the data to a QByteArray which accepts const char* and a size of type int in it's constructor as provided by Qt and the other program is supposed to return a number in the range 0-255 hence the unsinged char. If what I'm doing is wrong what's the best way to obtain the desired result?

Also call to program2->function invokes the following:

inline QDBusPendingReply<uchar> type()
    {
        QList<QVariant> argumentList;
        return asyncCallWithArgumentList(QLatin1String("type"), argumentList);
    }

this class has a function called value() which invokes:

inline typename Select<0>::Type value() const
    {
        return argumentAt<0>();
    }

NOTE: my main purpose of this question was to find out a way to create a reference to the returned result of calling a function defined within the proxy class not how to convert it to something acceptable for creation of a QByteArray so please stop adding unrelated tags like QByteArray or QtCore etc.


Solution

  • What you need is the address of the data... which would look like

    function2(& program2->function().value(), 1);
    

    and would be perfectly safe, since the lifetime of the temporary variable is until the end of the complete expression, which is long enough1 for function2 to use it.

    Unfortunately, you can't use the & address-of operator on an rvalue. But there is a workaround, since you can bind a const lvalue reference to an rvalue, and you want a pointer to const data anyway:

    template<T>
    const T* get_addr(const T& v) { return &v; }
    
    function2( get_addr(program2->function().value()), 1 );
    

    If you get a signed-vs-unsigned, mismatch, try

    function2( get_addr<char>(program2->function().value()), 1 );
    

    Of course this is a lot of work just to avoid giving the variable a name, as Captain Obvlious has done.


    1 Unless function2 saves that pointer for later, but if it does then you shouldn't be using a local variable to hold the value either, but pay very close attention to object lifetime.