C ++ 11模板代码-无法在XCode中编译

马丁·佩里

我有这个代码:

template<bool, class _Ty1, class _Ty2> struct MyIf
{   // type is _Ty2 for assumed false
    typedef _Ty2 type;
};

template<class _Ty1, class _Ty2> struct MyIf<true, _Ty1, _Ty2>
{   // type is _Ty1 for assumed true
    typedef _Ty1 type;
};

template<class _Ty>
struct my_decay
{
    // determines decayed version of _Ty
    typedef typename std::decay<_Ty>::type _Ty1;

    typedef typename MyIf<
        std::is_arithmetic<_Ty1>::value, typename _Ty1, //(1)
        typename MyIf<
            std::is_same<LuaString, _Ty1>::value, typename _Ty1, //(2)

        typename _Ty //(3)
        >::type
    >::type type;
};

在Visual Studio 2015下,它可以正常编译。但是,当我将代码移植到XCode时,出现以下错误:

  • 对于(1)=>在“类型名”之后需要一个限定名称
  • for(2)=>类型名称不允许指定存储类
  • 对于(3)=>类型名称不允许指定存储类

在Xcode中,我将C ++语言Dialcet设置为GNU ++ 14,将C ++标准库设置为C ++ 11支持,并且使用的编译器是Apple LLVM 8.1。

内森·奥利弗

my_decay Ty_Ty1中不是合格名称。这意味着您不需要typename在它们上使用看起来MSVS出于某种原因允许您这样做。要么是错误,要么是他们认为它是“功能”,因此人们可以在typename任何地方进行喷涂以使其正常工作。正确的版本应为:

template<class _Ty>
struct my_decay
{
    // determines decayed version of _Ty
    typedef typename std::decay<_Ty>::type _Ty1;

    typedef typename MyIf<
        std::is_arithmetic<_Ty1>::value, _Ty1, //(1)
        typename MyIf<
            std::is_same<void, _Ty1>::value, _Ty1, //(2)

        _Ty //(3)
        >::type
    >::type type;
};

另外,您应避免在标识符中使用前划线。系统会保留系统的前导下划线和大写字母,以及所有带双下划线的标识符。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章