3

I have declared:

class aaa {
public:
    static std::queue<QPair<void (*)( ... ), int> > m_commands;
    static int bbb();
    static void ccc(...);
};

and in bbb() method I wrote:

int aaa::bbb() {
    m_commands.push( qMakePair( aaa::ccc, 0 ) );
}

but it complains about:

error C2664: 'void std::queue<_Ty>::push(QPair<T1,T2> &&)' : cannot convert parameter 1 from 'QPair<T1,T2>' to 'QPair<T1,T2> &&'

why? When I had function like that:

void reg( void ( *invoker )( ... ), int args  ) {
    m_commands.push( qMakePair( invoker, args ) );
}

I could easily send to the above function a static function this way:

reg( aaa::ccc, 0 );
1
  • 4
    I'm pretty sure that passing a no-arg function pointer to that reg function doesn't compile; see ideone.com/gf8J3. Commented Jul 9, 2012 at 19:39

1 Answer 1

1

qMakePair( aaa::ccc, 0 ) is (presumably) returning a value of type QPair<void (*)(), int>, since it doesn't know that you want a value of type QPair<void (*)( ... ), int>. Invoke it explicitly as qMakePair<void (*)( ... ), int>( aaa::ccc, 0 ) or reinterpret_cast aaa::ccc to the desired function pointer type.

Not to mention that this is (almost certainly) illegal, as aaa::ccc does not have the correct signature and cannot be invoked through the function pointer type you're using.

Sign up to request clarification or add additional context in comments.

7 Comments

Whilst the pointer conversion is valid, attempting to dereference that pointer (i.e. call the function) leads to undefined behaviour.
@OliCharlesworth yes; AIUI it's legitimate to cast it back to the correct type and then indirect-call it.
Yes, that would be valid (although a little unorthodox!).
@OliCharlesworth I am sorry guys! Take my apologize. There was a bug in the post, I changed ccc( ... ). Now it compiles ideone.com/0vxpi anyway the explicitly invoking (qMakePair<void (*)( ... ), int>( aaa::ccc, 0 )) worked for me.
@ecatmur but now there's written ccc( ... ) as the m_commands wants, so why do I have to use qMakePair explicitly with template arguments?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.