[英]c++ template call of function pointer type
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++ 在C ++中
type_name()
is an expression that creates a default-initialized instance of type_name
. 是一个表达式,用于创建
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). 是一个有效的C ++语句(只是创建一个
int
并抛出它)。
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). 带有完全警告的
g++
发出诊断消息,因为它是一个可疑的(可能是非预期的)操作,但是代码是有效的,甚至可以有依赖它的程序(如果类型是用户定义的类型,并且实例的构造函数具有副作用)。
command();
It creates a temporary object like TYPE();
它创建一个像
TYPE();
的临时对象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. 你应该打开
-Wall
编译器的选项。 Live code. 实时代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.