关于MSVC和g ++如何处理C ++模板代码之间的差异

时间:2015-10-22 07:42:58

标签: c++ templates c++11 variadic-templates

我偶然发现了来自ACCU(http://accu.org/index.php/journals/1916)的这段代码,并且有兴趣在C ++中使用CRTP来实现一个有趣的项目。作者给出的代码如下:

namespace aop
{

template <class A>
class NullAspect
{};

template <template <template <class> class> class Base>
struct Decorate
{
private:
    struct None {};

    template <template <class> class ... Aspects>
    struct Apply;

    template <template <class> class T>
    struct Apply<T>
    {
        template <class E>
        using Type = T<E>;
    };

    template<template < class > class A1, template < class > class ... Aspects>
    struct Apply<A1, Aspects...>
    {
        template <class T>
        using Type = A1<typename Apply<Aspects...>::template Type<T>>; // the errors point to this line and the 'Type' refers to the 'template Type<T>'
    };

public:
    template<template <class> class ... Aspects>
    struct with
    {
        template <class T>
        using AspectsCombination = typename Apply<Aspects...>::template Type<T>;

        typedef AspectsCombination<Base<AspectsCombination>> Type;
    };
};
}

我尝试在Microsoft VS2015中编译它,它给了我以下一组错误:

Error   C2146   syntax error: missing '>' before identifier 'Type'  
Error   C2947   expecting '>' to terminate template-argument-list, found '<'    
Error   C2061   syntax error: identifier 'T'    
Error   C2238   unexpected token(s) preceding ';'   
Error   C1201   unable to continue after syntax error in class template definition  
Error   C2143   syntax error: missing ';' before '}'    
Error   C2238   unexpected token(s) preceding ';'   

我使用相同的代码,检查语法并用g ++编译它并编译好。我应该注意的两个编译器之间是否存在差异?这会使cl.exe产生这些错误的问题是什么?它们是由于cl.exe如何解析任何基于模板的代码?要使此代码在msvc上运行需要进行哪些更改?

编辑:

这里是作者提供的test.cpp的完整代码,可以帮助你们更清楚地了解:

#include <iostream>
#include <cmath>
#include "aop.h"

//#define INHERITING_CTORS  as of g++ 6.4.3, inheriting ctors was not implemented

template <typename _UnderlyingType>
struct Number
{
    template <template <class> class A = aop::NullAspect>
    class Type
    {
    public:
        typedef _UnderlyingType UnderlyingType;
        typedef A<Number::Type<A>> FullType;

        Type(UnderlyingType n)
            : n(n)
        {}

        friend std::ostream& operator<<(std::ostream& out, const Type& number)
        {
            return out << number.n;
        }
    protected:
        UnderlyingType n;
    };
};

template <class A>
class ArithmeticAspect: public A
{
public:
    typedef typename A::FullType FullType;

#ifdef INHERITING_CTORS
    using A::A;
#else
    ArithmeticAspect(typename A::UnderlyingType n)
        : A(n)
    {}

    ArithmeticAspect(const A& a)
        : A(a)
    {}
#endif

    FullType operator+(const FullType& other) const
    {
        FullType tmp(*this);
        return tmp += other;
    }

    FullType operator-(const FullType& other) const
    {
        FullType tmp(*this);
        return tmp -= other;
    }

    FullType operator+=(const FullType& other)
    {
        A::n += other.n;
        return A::n;
    }

    FullType operator-=(const FullType& other)
    {
        A::n -= other.n;
        return A::n;
    }

    // same for *, *=, /, /=
};

template <class A>
class IncrementalAspect: public A
{
public:
    typedef typename A::FullType FullType;

#ifdef INHERITING_CTORS
    using A::A;
#else
    IncrementalAspect(typename A::UnderlyingType n)
        : A(n)
    {}

    IncrementalAspect(const A& a)
        : A(a)
    {}
#endif

    FullType operator++(int)
    {
        FullType tmp(*this);
        operator++();
        return tmp;
    }

    FullType operator++()
    {
        ++A::n;
        return *this;
    }

    FullType operator--(int)
    {
        FullType tmp(*this);
        operator--();
        return tmp;
    }

    FullType operator--()
    {
        --A::n;
        return *this;
    }
};

/*
* Configurable Aspect sumExample
*/
template <unsigned int PRECISION>
struct RoundAspect
{
    template <class A>
    class Type : public A
    {
    public:
        typedef typename A::FullType FullType;

#ifdef INHERITING_CTORS
        using A::A;
#else
        Type(typename A::UnderlyingType n)
            : A(n)
        {}

        Type(const A& a)
            : A(a)
        {}
#endif

        FullType operator+(const FullType& other) const
        {
            return FullType(round(A::operator+(other).n));
        }

    private:
        static float round(float f)
        {
            const unsigned int e = std::pow(10, PRECISION);
            return float(int(f * e)) / e;
        }
    };
};

template <class A>
class LogicalAspect: public A
{
public:
    typedef typename A::FullType FullType;

#ifdef INHERITING_CTORS
    using A::A;
#else
    LogicalAspect(typename A::UnderlyingType n)
        : A(n)
    {}

    LogicalAspect(const A& a)
        : A(a)
    {}
#endif

    bool operator!() const
    {
        return !A::n;
    }

    bool operator&&(const FullType& other) const
    {
        return A::n && other.n;
    }

    bool operator||(const FullType& other) const
    {
        return A::n || other.n;
    }
};

template <class A>
class BitwiseAspect: public A
{
public:
    typedef typename A::FullType FullType;

#ifdef INHERITING_CTORS
    using A::A;
#else
    BitwiseAspect(typename A::UnderlyingType n)
        : A(n)
    {}

    BitwiseAspect(const A& a)
        : A(a)
    {}
#endif

    bool operator~() const
    {
        return ~A::n;
    }

    FullType operator&(const FullType& mask) const
    {
        return A::n & mask.n;
    }

    FullType operator|(const FullType& mask) const
    {
        return A::n | mask.n;
    }

    FullType operator<<(const FullType& bitcount) const
    {
        return A::n << bitcount.n;
    }

    FullType operator>>(const FullType& bitcount) const
    {
        return A::n >> bitcount.n;
    }

    FullType& operator>>=(const FullType& bitcount)
    {
        A::n >>= bitcount.n;
        return *static_cast<FullType*>(this);
    }
};

template <class N>
void sumExample(typename N::UnderlyingType n1, typename N::UnderlyingType n2)
{
    N a(n1);
    N b(n2);
    N c = a + b;
    std::cout << c << std::endl;
}

template <class N>
void orExample(typename N::UnderlyingType n1, typename N::UnderlyingType n2)
{
    N a(n1);
    N b(n2);
    std::cout << (a || b) << std::endl;
}

template <class N>
void bitwiseExample(typename N::UnderlyingType n1, typename N::UnderlyingType n2)
{
    N a(n1);
    N b(n2);
    std::cout << (a + ((b >>= 1) << 3)) << std::endl;
}

int main()
{

    typedef aop::Decorate<Number<unsigned int>::Type>::with<ArithmeticAspect, IncrementalAspect, LogicalAspect, BitwiseAspect>::Type IntegralNumber;
    bitwiseExample<IntegralNumber>(1, 2);
    sumExample<IntegralNumber>(1, 2);

    typedef aop::Decorate<Number<float>::Type>::with<RoundAspect<2>::Type, ArithmeticAspect, LogicalAspect>::Type FloatRoundLogicalNumber;
    orExample<FloatRoundLogicalNumber>(1, 0);

    typedef aop::Decorate<Number<int>::Type>::with<LogicalAspect>::Type IntLogicalNumber;
    orExample<IntLogicalNumber>(1, 0);

    typedef aop::Decorate<Number<float>::Type>::with<RoundAspect<2>::Type, ArithmeticAspect>::Type FloatRoundNumber;
    sumExample<FloatRoundNumber>(1.339, 1.1233);

    return 0;
}

2 个答案:

答案 0 :(得分:3)

恕我直言,它是模板化模板上的>>综合症。在C ++ 11之前,需要一个空格来分隔>令牌而不是一个>>令牌。

从C ++ 11开始,n4296草案在14.2中说明了模板特化的名称[temp.names]§3:

  

...同样,第一个非嵌套&gt;&gt;被视为连续两次但是   不同的&gt;令牌,第一个作为模板参数列表的结尾并完成   模板id

看起来MSVC2015还没有实现那部分标准(或者您可能忘记声明源代码的C ++版本)

为了完整起见,在没有指定std=c++11的情况下使用CLang 3.4.1进行编译时,它会显示此错误:

error: a space is required between consecutive right angle brackets (use '> >')
    using Type = A1<typename Apply<Aspects...>::template Type<T>>; // the er...

并希望在C ++ 11模式下甚至不会发出警告......

(*)遗憾的是,我无法访问VS2015,微软在C++11/14/17 Features页面声明了Right angle brackets由VS2013和2015实施的https://msdn.microsoft.com/en-us/library/59dhz064.aspx。所以我假设应该在项目的某个地方属性代码级指示。

答案 1 :(得分:0)

所以在摆弄并尽可能地阅读之后,我终于给原作者发了一封电子邮件。因此,Hugo Arregui先生给予了他所有的荣誉。

关于cl抱怨的行,修复方法如下:

template < template <class> class A1, template <class> class A2, template <class> class... Aspects>
struct Apply<A1,A2,Aspects...>
{
    template<class T>
    using Type = A1< typename Apply<A2, Aspects...>::template Type<T> >;
};

如果有人知道为什么Microsoft的编译器要求我们在模板参数中扩展至少2个模板类并在此给出解释,那将是很好的。正在与作者交谈,他也很惊讶。