(这里的Noob警报--我不是真正的C++程序员,我只是发现自己需要重新实现一些C++代码。)
我试图从下列功能中理解OpenFST,作为在JOpenFST中读取OpenFST二进制文件的工作的一部分:
template <class T,
typename std::enable_if<std::is_class<T>::value, T>::type* = nullptr>
inline std::istream &ReadType(std::istream &strm, T *t) {
return t->Read(strm);
}我无法确定在这个模板声明中什么可以保证Read在t上的存在。我意识到我对enable_if和is_class的理解是模糊的,但我看不出有什么能提供这样的方法。
也许它来自更广泛的背景?为所有类类型声明Read的东西?下面是该函数驻留在util.h中的导入:
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <fst/compat.h>
#include <fst/types.h>
#include <fst/log.h>
#include <fstream>
#include <fst/flags.h>
#include <unordered_map>感谢您对困惑的Java的耐心。
发布于 2019-01-19 22:07:33
我无法确定在这个模板声明中什么可以保证
Read在t上的存在。
没有什么能保证存在。
也就是说:如果模板是用没有T成员函数的Read实例化的,那么编译器会抱怨调用了一个不存在的函数。
从另一个角度来看,模板是错误的,除非T::Read存在(并且可以用给定的参数调用),这一事实保证了模板的任何格式良好的实例化中的T都有这样的成员。
https://stackoverflow.com/questions/54271751
复制相似问题