sunnyeo
sunnyeo

Reputation: 35

no type named ‘type’ in ‘struct boost::enable_if<boost::is_pod<T>, void>’

I'm trying to compile this repo in Linux, but I have some trouble in this step.

*util/BinarySerialization.hpp

template <typename T> inline
typename boost::enable_if<boost::is_pod<T>, void>::type
writeBinary(const T& data, std::ostream& outputStream)
{
    ...
}

template <typename T> inline
typename boost::enable_if<boost::is_pod<T>, void>::type
writeBinary(const std::vector<T>& data, std::ostream& outputStream)
{
    ...
}

template <typename T> inline
typename boost::disable_if<boost::is_pod<T>, void>::type
writeBinary(const std::vector<T>& data, std::ostream& outputStream)
{
    // write vector length
    size_t length = data.size();
    outputStream.write(reinterpret_cast<const char*>(&length), sizeof(length));
    if (!outputStream) throw IOException();
    // write vector data one by one
    for (size_t i = 0; i < length; ++i)
        writeBinary(data[i], outputStream); // error!
}

void writeBinary(const std::string& data, std::ostream& outputStream);

*serialization/ElementBS.cpp

void bil::writeBinary(const Element& data, std::ostream& outputStream)
{
    writeBinary(data.m_name, outputStream);
    writeBinary(data.m_options, outputStream);
}

*xdlrc/model/Element.hpp

typedef std::vector<std::string> ConfigurationOptions;

class Element {
public:
    Element();

    std::string& name();
    const std::string& name() const;
    ConfigurationOptions& options();
    const ConfigurationOptions& options() const;
    void clear();

private:
    friend void writeBinary(const Element& data, std::ostream& outputStream);
    friend void readBinary(Element& data, std::istream& inputStream);

    std::string m_name;
    ConfigurationOptions m_options;

};

util/BinarySerialization.hpp: In instantiation of 'typename boost::disable_if<boost::is_pod<T>, void >::type bil::writeBinary(const std::vector<T>&, std::ostream&) [with T = std::__cxx11::basic_string<char>; typename boost::disable_if<boost::is_pod<T>, void>::type = void; std::ostream = std::basic_ostream<char>]':
serialization/ElementBS.cpp:16:45: required from here
util/BinarySerialization.hpp:78:21: error: no matching function for call to 'writeBinary(const value_type&, std::ostream&)'
          writeBinary(data[i], outputStream);
...
BinarySerialization.hpp:31:5: error: no type named 'type' in 'struct boost::enable_if<boost::is_pod<std::__cxx11::basic_string<char> >, void>'

The first writeBinary in ElementBS.cpp match with the last function in BinarySerialization.hpp, and the 2nd function in ElementBS.cpp match with the 3rd function. But, writeBinary(data[i], outputStream); in 3rd function couldn't match any functions. I don't know how to fix it;

Upvotes: 2

Views: 6103

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136515

To fix the compiler error make sure declaration void writeBinary(const std::string& data, std::ostream& outputStream); precedes writeBinary function templates.

This is because during template instantiation, the second phase of dependent name lookup only argument-dependent name look up is performed. Since arguments to writeBinary(const std::string& data, std::ostream& outputStream) come from namespace std the argument-dependent name lookup never finds this overload because it is not in namespace std.

By declaring that function prior to function templates that use it you make that function available during the first phase of the two-phase lookup. This name is then considered during function template instantiation, the second phase of name lookup.

Upvotes: 2

Related Questions