C++ C++;模板:不能与上一个';键入名称';声明说明符

C++ C++;模板:不能与上一个';键入名称';声明说明符,c++,templates,C++,Templates,我正在编写一个实用函数来查找容器中的元素,如下所示: #include <algorithm> #include <type_traits> // Helper to determine whether there's a const_iterator for T. template <typename T> struct hasConstIt { private: template<typename C> static char tes

我正在编写一个实用函数来查找容器中的元素,如下所示:

#include <algorithm>
#include <type_traits>

// Helper to determine whether there's a const_iterator for T.
template <typename T>
struct hasConstIt {
private:
    template<typename C> static char test(typename C::const_iterator*);
    template<typename C> static int test(...);

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

// Check if a container contains an element.
template <typename Container>
typename std::enable_if<hasConstIt<Container>::value, void>::type
struct In {
    const Container & container;
    typename Container::value_type const & element;

    In(const Container & container, typename Container::value_type const & element) :
        container(container), element(element) {}

    bool operator()() const {
        return std::find(container.begin(), container.end(), element) != container.end();
    }
};
我一直在寻找解决方案,但找不到。编译器为什么在这里抱怨?

模板
typename std::enable_if::type
结构{
这是错误的。这是作为

模板
结构{
在老式的C++11中,注意第二个参数:它有默认值,通常从不显式指定,但在谓词不满足时阻止实例化

或者更现代一些,

模板中需要(hascontit::value)结构{

在C++20中。

我从未见过在类型声明中使用
enable_if
。这可能吗?我只在函数中使用过它。也许你只想在
结构定义的顶部使用
静态断言(hascostint::value);
。@FrançoisAndrieux这似乎很管用!谢谢
./util.hpp:19:1: error: cannot combine with previous 'type-name' declaration specifier
struct In {
^
./util.hpp:18:1: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
typename std::enable_if<hasConstIt<Container>::value, void>::type