提升不返回类型

Boost at not returning a type

本文关键字:返回类型      更新时间:2023-10-16

我有以下代码编译失败:

#include <iostream>
#include "boost/mpl/set.hpp"
#include "boost/mpl/at.hpp"
#include "boost/type_traits/is_same.hpp"
struct TypeSet {
    typedef boost::mpl::set<int, float> typeset;
    template<typename T>
    static bool hasType()
    {
        using namespace boost;
        using namespace boost::mpl;
        return is_same< at< typeset, T >::type, T >::value; // <-- ERROR IS HERE
    }  
};

int main(int argc, const char * argv[])
{
    bool hasInt = TypeSet::hasType<int>();
    std::cout << (hasInt ? "set contains int" : "set does not contain int") << std::endl;    
    return 0;
}

代码正在使用Apple LLVM clang 4.1编译器和boost 1.5.2进行编译,错误是"类型参数的模板参数必须是类型" - 基本上编译器抱怨boost::mpl::at没有返回类型。有问题的代码几乎是从 boost 文档中逐字获取的,所以我不知道这有什么问题(据我所知boost::mpl::at确实返回了一个类型)。

你需要

typename at< typeset, T >::type

因为它取决于模板参数T.因此,您必须告诉编译器type是此上下文中的一种类型。