Pointers to functions
The increment and decrement operators are not valid for pointers to functions.
Here's the relevant text from the C++ Draft Standard (N3337):
5.3.2 Increment and decrement
1 The operand of prefix ++
is modified by adding 1
, or set to true
if it is bool
(this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The value is the new value of the operand; it is an lvalue. If x
is not of type bool
, the expression ++x
is equivalent to x+=1
.
The type of a pointer to a function is neither an arithmetic type nor is such a pointer a pointer to a completely-defined object type. Hence, ++
cannot be applied to a pointer to a function.
As far C is concerned, the C99 Standard (n1256) says:
6.5.3.1 Prefix increment and decrement operators
1 The operand of the prefix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue.
2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1). See the discussions of additive operators and compound assignment for information on constraints, types, side effects, and conversions and the effects of operations on pointers.
and
6.5.6 Additive operators
2 For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)
Here again, a pointer to a function cannot be incremented for the same reasons.
Array of pointers to functions / Pointers to pointers to functions
The line
int(*fp[2])(int) = {f1,f2};
declares an array of pointers to functions. Hence, the expressions fp[0]
and fp[1]
are valid expressions. You can have a pointer to a pointer to function object that be used with the increment operator.
int (**fpp)(int) = fp;
(*fpp)(10); // Calls f1(10)
fpp[0](10); // Calls f1(10)
fpp[1](10); // Calls f2(10)
++fpp;
(*fpp)(20); // Calls f2(20)
fpp[0](20); // Calls f2(20)
Unfortunately, gcc 4.7.3 allows the following statements to compile while g++ does not.
int(*fp[2])(int) = {f1,f2};
int (*fpp)(int) = fp;
Calling
fpp(10);
leads to undefined behavior in such a case.
error: no matching function for call to ‘std::runtime_error::runtime_error()
, which is a good thing, becausestd::runtime_error
has no default constructor. Your compiler is just broken, write to your vendor and demand a fix. Anyway, you should fix every warning, even the most pointless ones, because if you routinely allow warnings, you might miss one which is not pointless./Wall
results in warnings from the standard headers such as<stdio.h>
. Fixing them would mean modifying the vendor's headers, possibly introducing incompatibilities or even breaking inline functions because the behavior resulting from the code wasn't understood. Compiling with/W3
seems to work better. This has happened in multiple releases, and I'm not sure they will be fixed. The usual answer has the tone of, "These are warnings, not errors."