0

I want to supress an unsued parameter warning within the code.

My first approach was:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent *UNUSED(event))
{
    MousebuttonHold = false;
    updateGL();
    return;
}

where the definitions are as followed:

#define UNUSED(NAME) USE_IT(NAME)
#define USE_IT(NAME) UNUSED_ ## NAME

This didn't work. After a bit of SO research I figgured out that in C++ I simply could do:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent)
{/*...*/}

Well this fixed the problem with the warning perfectly, but now.... the mouseReleaseEvent() Isn't triggered anymore at all. So this also isn't working for me.

So what other ways I do have for supressing the warning with a in code variant?

5
  • Besides of anonymous parameters, (void)NAME; is a common idiom to mark a variable as unused (while technically using it in a senseless way). So you want to #define UNUSED(NAME) ((void)NAME)
    – leemes
    Jul 17, 2015 at 10:23
  • @leemes: but that had to be done in the function body right? I can't imagine it would work in the param list. because it would throw in an extra type.
    – dhein
    Jul 17, 2015 at 10:24
  • Right, in the body. Sorry, I didn't see how you use the macro in your example, I thought you were talking about using it in the body. - Ah I now see that you use Qt. There is a macro already: Q_UNUSED ;)
    – leemes
    Jul 17, 2015 at 10:25
  • @leemes: But I want to use it in the parameter list. thanks for your input anyway :)
    – dhein
    Jul 17, 2015 at 10:29
  • Then, besides of anonymous parameters, you could try to add an __attribute__((unused)) after the parameter name, or disable the warning at least during the function signature (by adding compiler-specific pragmas before / after the signature). But both solutions are non-standard.
    – leemes
    Jul 17, 2015 at 10:33

2 Answers 2

9

Solution #1 – Unnamed parameter:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent *)

Solution #2 – Qt Q_UNUSED macro:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    // Your code...
}

Actually, this macro is the define for the next method:

Solution #3 – C way:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    // Your code...
}
4

This is because the parameter of QWidget::mouseReleaseEvent is a pointer. You must keep the *:

void RenderGraphFrame::mouseReleaseEvent(QMouseEvent*)
{
  // Your code
}
1
  • yes, the dreaded overloading resolution with optional virtual declaration induce such problems... one of the weak C++ points...
    – CapelliC
    Jul 17, 2015 at 11:05

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.