C 函子初始化
C++ functor initialization
本文关键字:初始化 更新时间:2023-10-16
我无法在以下代码示例中理解 {}
的目的。为什么不做cell_type(...)
而不是cell_type{}(...)
?我只是在这里放了一个简化的版本,希望显示足够的上下文。原始代码位于https://github.com/pytorch/pytorch/blob/master/master/aten/src/aten/native/rnnn.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{}
构建了cell_type
的临时实例。假设cell_type
公开了operator()
,则需要一个实例来调用它 - 因此,您不能简单地说cell_type()
。例如
struct cell_type { void operator()() { } };
cell_type{}(); // OK, creates temporary instance and invokes it
cell_type(); // Creates temporary instance, but doesn't invoke it
我的猜测是DEFINE_QUANTIZED_RNN_CELL
期望类型而不是实例,这就是为什么它使用{}
。
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 没有用于初始化C++中的变量模板的匹配构造函数
- 在未初始化映射的情况下,将值插入到映射的映射中
- C++成员初始化
- 为什么在C++中首先初始化成员类
- 同时具有"聚合初始化"和"模板推导"
- 初始化具有非默认构造函数的std::数组项的更好方法
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 在C和C++中初始化结构中的数组
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 在函数内部的声明中初始化数组,并在外部使用它
- 继承:构造函数,初始化C++11中基类的类C数组成员