Search code examples
c++openfst

Where does `Read` come from in this OpenFST function?


(Noob alert here---I'm not really a C++ programmer, just finding myself needing to reimplement some C++ code in Java.)

I am attempting to understand the following function from OpenFST as part of an effort to enable reading of OpenFST binary files in JOpenFST:

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);
}

I can't determine what in this template declaration guarantees the existence of Read on t. I realize my understanding of enable_if and is_class are fuzzy, but I don't see what there could provide such a method.

Perhaps it comes from the broader context? Something that declares a Read for all class types??? Here are the imports in util.h where that function resides:

#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>

Thank you for your patience with a confused Java dev.


Solution

  • I can't determine what in this template declaration guarantees the existence of Read on t.

    Nothing guarantees the existence.

    That is: If the template is instantiated with a T which doesn't have a Read member function, then the compiler will complain that a non-existent function was called.

    From another perspective, the fact that the template is ill-formed unless T::Read exists (and is invocable with given argument) guarantees that T in any well formed instantiation of the template will have such member.