If I have type declarations like
typedef void (*command)();
template <command c>
void execute() {
c();
}
void task() { /* some piece of code */ }
then
execute<task>();
will compile and behaves as expected. However if I define the template as
template <command c>
void execute() {
command();
}
it still compiles. I did this by accident. Now I am confused of what the second version would be expected to do.
In C++
type_name()
is an expression that creates a default-initialized instance of type_name
.
For natives types there are implicitly defined default constructors, so for example
int();
is a valid C++ statement (just creates an int
and throws it away).
g++
with full warnings on emits a diagnostic message because it's a suspect (possibly unintended) operation, but the code is valid and there can even be programs depending on it (if the type is a user-defined type and the constructor of the instance has side effects).
command();
It creates a temporary object like TYPE();
and compiler omits it as an unused variable.
warning: statement has no effect [-Wunused-value]
command();
^
You should turn on -Wall
compiler's option. Live code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With