[英]Explanation on c++ template syntax
I was looking at the following post write a boost::multi_array to hdf5 dataset and couldn't understand the syntax used for the templates in the first answer by Leo Goodstadt. 我在下面的帖子中向hdf5数据集编写一个boost :: multi_array,无法理解Leo Goodstadt在第一个答案中用于模板的语法。 Unfortunately I have not enough reputation to comment directly on the answer given there, that's why i've created this separate question. 不幸的是,我没有足够的声誉来直接评论那里给出的答案,这就是为什么我创建了这个单独的问题。
As far as I understood this there's a struct created with a static method type() that returns the respective type. 据我了解,有一个用静态方法type()创建的结构返回了各自的类型。
This generic struct is then overwritten by one with a fixed type. 然后,该泛型结构将被固定类型的结构覆盖。 But what exactly is written there in the curly braces , ie what is its meaning? 但是花括号中到底写了什么,即含义是什么? For example, what is the meaning of the following: 例如,以下含义是什么:
{ H5::IntType type { H5::PredType::NATIVE_CHAR
};
};
within the first definition of the char-template: 在char模板的第一个定义内:
template<> struct get_hdf5_data_type<char> {
H5::IntType type {
H5::PredType::NATIVE_CHAR
};
};
I've copied below the essential parts of the definition of the template, but probably it's easier to look at the original post for a clear picture. 我已经在模板定义的主要部分下面复制了内容,但可能更容易查看原始文章以获得清晰的图片。
template<typename T> struct get_hdf5_data_type {
static H5::PredType type() {
//static_assert(false, "Unknown HDF5 data type");
return H5::PredType::NATIVE_DOUBLE;
}
};
template<> struct get_hdf5_data_type<char> {
H5::IntType type { H5::PredType::NATIVE_CHAR };
};
template<> struct get_hdf5_data_type<long long> {
H5::IntType type { H5::PredType::NATIVE_LLONG };
};
template<> struct get_hdf5_data_type<unsigned long long> {
H5::IntType type { H5::PredType::NATIVE_ULLONG };
};
template<> struct get_hdf5_data_type<int8_t> {
H5::IntType type { H5::PredType::NATIVE_INT8 };
};
The curly syntax is initialisation 卷曲的语法是初始化
H5::IntType type { H5::PredType::NATIVE_CHAR };
Is similar to 类似于
H5::IntType type = H5::PredType::NATIVE_CHAR;
I'm not familiar with H5, but the OP then uses it as hdf_data_type.type
which is just refering to a particular type previously defined in your list of specialisations. 我对H5不熟悉,但是OP随后将其用作hdf_data_type.type
,它仅hdf_data_type.type
先前在您的专业列表中定义的特定类型。
Now hdf_data_type
needs to be of one of the provided template specialisations, get_hdf5_data_type<char>
for instance to provide the definition for type. 现在, hdf_data_type
必须是所提供的模板专业之一, get_hdf5_data_type<char>
才能提供类型的定义。
The commented out static_assert in the generic template could be used as a marker that no known specialisation has been found and the object hdf_data_type
is not useful. 通用模板中注释掉的static_assert可用作标记,表明找不到已知的专业化对象,并且对象hdf_data_type
无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.