我有以下无法编译的代码:
#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
在这种情况下是一种类型。