MSVC2010的许多boost模板错误

many boost template errors with MSVC2010

本文关键字:错误 boost 许多 MSVC2010      更新时间:2023-10-16

这是代码片段:

#include <boost/utility.hpp>
#include <boost/type_traits.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/typeof/typeof.hpp>
enum ScriptVarType_t { SVT_BOOL, SVT_STRING, SVT_CUSTOM, SVT_CustomWeakRefToStatic };
struct ScriptVar_t {};
struct CustomVar {
    struct Ref {};
    struct WeakRef {};
};
template<typename T> struct GetType;
template<typename T> struct _GetTypeSimple {
    typedef T type;
    static type defaultValue() { return T(); }
    static const type& constRef(const type& v) { return v; }
};
template<> struct GetType<bool> : _GetTypeSimple<bool> { static const ScriptVarType_t value = SVT_BOOL; };
template<> struct GetType<std::string> : _GetTypeSimple<std::string> { static const ScriptVarType_t value = SVT_STRING; };
template<> struct GetType<CustomVar::Ref> {
    typedef CustomVar::Ref type;
    static const ScriptVarType_t value = SVT_CUSTOM;
    static type defaultValue() { return type(); }
    static const type& constRef(const type& v) { return v; }
};

template<typename T>
struct CustomVarWeakRefType {
    typedef CustomVar::WeakRef type;
    static const ScriptVarType_t value = SVT_CustomWeakRefToStatic;
    static CustomVar::Ref defaultValue() { return T().getRefCopy(); }
    static CustomVar::WeakRef constRef(const T& v) { return v.thisRef.obj; }
};
struct StringType : GetType<std::string> {};
template<typename T>
struct _SelectType {
    static CustomVarWeakRefType<T>* selectType(const CustomVar&) { return NULL; }
    static StringType* selectType(const char*) { return NULL; }
    static StringType* selectType(char[]) { return NULL; }
    typedef typename BOOST_TYPEOF(*selectType(*(T*)NULL)) type;
};
template<typename T> struct GetType : _SelectType<T>::type {};
template<typename T>
T _CastScriptVarConst(const ScriptVar_t& s, T*, typename boost::enable_if_c<(GetType<T>::value < SVT_CUSTOM), T>::type*) {
    return (T) s;
}
template<typename T>
T _CastScriptVarConst(const ScriptVar_t& s, T*, typename boost::enable_if_c<boost::is_base_of<CustomVar,T>::value, T>::type*) {
    return *s.as<T>();
}

一些错误:

3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(416): error C2059: Syntaxfehler: ')'
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(416): error C2143: Syntaxfehler: Es fehlt ',' vor ')'
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(416): error C2947: ">" wird erwartet, um template-argument-list abzubrechen. ">" wurde gefunden.
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(416): warning C4346: 'GetType<T>::value<T>::type': Abhängiger Name ist kein Typ
3>          Präfix mit 'typename' zum Angeben eines Typs
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(416): error C2059: Syntaxfehler: ')'
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(421): error C2059: Syntaxfehler: ','
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(421): warning C4346: 'boost::is_base_of<CustomVar,T>::value': Abhängiger Name ist kein Typ
3>          Präfix mit 'typename' zum Angeben eines Typs
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(421): error C2143: Syntaxfehler: Es fehlt ',' vor ')'
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(421): error C2143: Syntaxfehler: Es fehlt ';' vor '{'
3>c:usersalbert zeyerprogrammierungopenlieroxincludeCScriptableVars.h(423): error C2143: Syntaxfehler: Es fehlt ';' vor '}'

(顺便说一句,有没有办法用英语获得这些消息?我是MSVC的新手。例如,它说:">"应该取消模板参数列表,但找到了">"。)

错误消息大多是愚蠢或错误的。

为什么?我甚至不知道微软风投在什么方面挣扎。

我该如何解决它?

好的,找到问题了。enable_if_c<(GetType<T>::value < SVT_CUSTOM), T>中较少的比较使编译器感到困惑,因为它不能真正知道value是一个整数。

我把它改成了GetType<T>::value <= SVT_CUSTOM-1,一切都很好。