[英]C++ functor initialization
I am not able to understand the purpose of {}
in the following code example. 在以下代码示例中,我无法理解{}
的用途。 Why not jus do cell_type(...)
instead of cell_type{}(...)
? 为什么不用cell_type(...)
代替cell_type{}(...)
? I just put a simplified version here hoping to show enough context. 我只是在这里放了一个简化版本,希望显示足够的上下文。 The original code is in https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/RNN.cpp#L781 in case you want more information. 如果您需要更多信息,原始代码位于https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/RNN.cpp#L781 。
#define DEFINE_QUANTIZED_RNN_CELL(..., cell_type, ... ) \
...
# what's the purpose of {} in the following line? \
return cell_type{}( \
...); \
}
using quantized_lstm_cell_type = LSTMCell<QuantizedCellParams>;
DEFINE_QUANTIZED_RNN_CELL(..., quantized_lstm_cell_type, ...);
template <typename cell_params>
struct LSTMCell {
using hidden_type = std::tuple<Tensor, Tensor>;
hidden_type operator()(...) const override {
...
}
};
cell_type{}
constructs a temporary instance of cell_type
. cell_type{}
构造一个cell_type
的临时实例。 Assuming that cell_type
exposes a operator()
, you need an instance to invoke that - therefore you cannot simply say cell_type()
. 假设cell_type
公开一个operator()
,则需要一个实例来调用该operator()
-因此,您不能简单地说出cell_type()
。 Eg 例如
struct cell_type { void operator()() { } };
cell_type{}(); // OK, creates temporary instance and invokes it
cell_type(); // Creates temporary instance, but doesn't invoke it
My guess is that DEFINE_QUANTIZED_RNN_CELL
expects a type and not an instance, this is why it uses {}
. 我的猜测是DEFINE_QUANTIZED_RNN_CELL
需要类型而不是实例,这就是为什么使用{}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.