Search code examples
c++unary-operator

Unary + operator on an int&


I have following statement and it compiles:

static unsigned char CMD[5] = {0x10,0x03,0x04,0x05,0x06};

int Class::functionA(int *buflen)
{
    ...
    int length = sizeof(CMD); + *buflen; // compiler should cry! why not?
    ...
}

Why I get no compiler error?


Solution

  • + *buflen;
    

    Is a valid application of the unary + operator on an int&, it's basically a noop. It's the same as if you wrote this:

    int i = 5;
    +i; // noop
    

    See here for what the unary operator+ actually does to integers, and here what you can practically do with it.