【问题标题】:no type named ‘type’ in ‘struct boost::enable_if<boost::is_pod<T>, void>’“struct boost::enable_if<boost::is_pod<T>, void>”中没有名为“type”的类型
【发布时间】:2018-10-09 10:48:57
【问题描述】:

我正在尝试在 Linux 中编译这个 repo,但是这一步遇到了一些麻烦。

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

*序列化/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: 在 'typename boost::disable_if<:is_pod>, void >::type bil::writeBinary(const std::vector&, std:: ostream&) [with T = std::__cxx11::basic_string;类型名 boost::disable_if<:is_pod>, void>::type = void; std::ostream = std::basic_ostream]':
序列化/ElementBS.cpp:16:45:从这里需要
util/BinarySerialization.hpp:78:21: 错误: 没有匹配函数调用'writeBinary(const value_type&, std::ostream&)'
writeBinary(data[i], outputStream);
...
BinarySerialization.hpp:31:5: 错误: 'struct boost::enable_if<:is_pod>>, void>'中没有​​名为'type'的类型

ElementBS.cpp 中的第一个 writeBinary 与 BinarySerialization.hpp 中的最后一个函数匹配,ElementBS.cpp 中的第二个函数与第三个函数匹配。但是, writeBinary(data[i], outputStream);在第三个功能无法匹配任何功能。我不知道如何解决它;

【问题讨论】:

  • void writeBinary(const std::string&amp; data, std::ostream&amp; outputStream); 我只在这里看到了声明。函数定义在哪里?
  • 函数定义在util/BinarySerialization.cpp中。

标签: c++ boost


【解决方案1】:

要修复编译器错误,请确保声明 void writeBinary(const std::string&amp; data, std::ostream&amp; outputStream); 位于 writeBinary 函数模板之前。

这是因为在模板实例化期间,依赖名称查找的第二阶段仅执行参数依赖名称查找。由于 writeBinary(const std::string&amp; data, std::ostream&amp; outputStream) 的参数来自命名空间 std,因此依赖于参数的名称查找永远不会找到此重载,因为它不在命名空间 std 中。

通过在使用它的函数模板之前声明该函数,您可以使该函数在两阶段查找的第一阶段可用。然后在函数模板实例化(名称查找的第二阶段)期间考虑此名称。

【讨论】:

    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多