c++ - 修复第三方代码 : "error: ‘enable_if’ in namespace ‘std’ does not name a template type"

标签 c++ c++11 templates

在尝试构建第三方应用程序时,我发现其依赖项之一 ( https://github.com/genome/joinx ) 无法使用较新的编译器进行干净地编译。在整理它的过程中(代码看起来多年来一直没有维护),我遇到了一个我无法完全弄清楚的编译器错误。

问题出在这个文件中:


#pragma once

// Utilities for comparing things / establishing orderings.

#include <boost/utility/declval.hpp>

///////////////////////////////////////////////////////////////////////////
// Tags for use with enable_if

// Compare objects return an int <0, =0, or >0 to establish an ordering
// (cf. strcmp)
struct CompareBase {};
// ComparePred objects are predicates (e.g., less than)
// (cf. std::less)
struct ComparePredBase {};

template<typename Op>
struct DerefBinaryOp {
    Op op;

    explicit DerefBinaryOp(Op op = Op())
        : op(op)
    {}

    template<typename ValueType>
    auto operator()(ValueType const& x, ValueType const& y) -> decltype(op(*x, *y)) {
        return op(*x, *y);
    }
};

// Given a "Compare" function, convert it to a less than predicate.
template<
          typename T
        , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
        >
struct CompareToLessThan : ComparePredBase {
    CompareToLessThan(T cmp = T())
        : cmp(cmp)
    {}

    template<typename U>
    bool operator()(U const& x, U const& y) const {
        return cmp(x, y) < 0;
    }

    T cmp;
};

// Given a "Compare" function, convert it to a greater than predicate.
template<
          typename T
        , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
        >
struct CompareToGreaterThan : ComparePredBase {
    CompareToGreaterThan(T cmp = T())
        : cmp(cmp)
    {}

    template<typename U>
    bool operator()(U const& x, U const& y) const {
        return cmp(x, y) > 0;
    }

    T cmp;
};

// This is a helper class that defines relational operators
// (>, <, ==, !=, ...) for simple structs that define value_type
// and have a single member: value_type value;
template<typename T>
struct ValueBasedRelOps {
    friend bool operator<(T const& lhs, T const& rhs) {
        return lhs.value < rhs.value;
    }

    friend bool operator>(T const& lhs, T const& rhs) {
        return lhs.value > rhs.value;
    }

    friend bool operator<=(T const& lhs, T const& rhs) {
        return lhs.value <= rhs.value;
    }

    friend bool operator>=(T const& lhs, T const& rhs) {
        return lhs.value >= rhs.value;
    }

    friend bool operator==(T const& lhs, T const& rhs) {
        return lhs.value == rhs.value;
    }

    friend bool operator!=(T const& lhs, T const& rhs) {
        return lhs.value != rhs.value;
    }
};

我的编译器吐出错误:

/home/einar/Download/Sources/joinx/src/lib/common/RelOps.hpp:34:36: error: ‘enable_if’ in namespace ‘std’ does not name a template type
   34 |         , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
      |                                    ^~~~~~~~~
/home/einar/Download/Sources/joinx/src/lib/common/RelOps.hpp:34:45: error: expected ‘>’ before ‘<’ token
   34 |         , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
      |                                             ^
/home/einar/Download/Sources/joinx/src/lib/common/RelOps.hpp:52:36: error: ‘enable_if’ in namespace ‘std’ does not name a template type
   52 |         , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
      |                                    ^~~~~~~~~
/home/einar/Download/Sources/joinx/src/lib/common/RelOps.hpp:52:45: error: expected ‘>’ before ‘<’ token
   52 |         , typename = typename std::enable_if<std::is_base_of<CompareBase, T>::value>::type
      |                                             ^

我不熟悉 std::enable_if 并且之前对 SO 的搜索没有产生与我的问题类似的解决方案。错误的确切原因是什么,所以我可以从那里着手修复它?

最佳答案

为了帮助您在未来“找到鱼”而不是给鱼:

error: ‘enable_if’ in namespace ‘std’ does not name a template type意味着 enable_if 很可能在那个使用点对编译器来说是未知的。

搜索“std::enable_if”最终会将您带到此链接 https://en.cppreference.com/w/cpp/types/enable_if

您会注意到 Defined in header <type_traits> 这行.

这会导致问题“是否已包含此 header ?”。

关于c++ - 修复第三方代码 : "error: ‘enable_if’ in namespace ‘std’ does not name a template type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57074289/

相关文章:

C++:如何使用时间和本地时间获取实际时间?

c++ - 如何将 _RICHEDIT_VER (riched20.dll) 升级到版本 3 或更高版本

c++11 - 组合模板类型的继承

c++ - 用于智能指针的 std::atomic 的部分模板特化

html - 寻找像这样的 wordpress 的三面板 css 布局

c++ - 我使用的 std::string 的实现是否实现了引用计数?

c++ - 修改窗口的文本框控件的文本

没有模板参数的 C++ 模板?

c++ - C++ 中的可扩展自动类注册

c++ - 将所有模板类型传递给运算符而不指定所有类型