gpt4 book ai didi

c++ - 关于 MSVC 和 g++ 处理 C++ 模板代码的区别

转载 作者:行者123 更新时间:2023-11-30 02:35:58 26 4
gpt4 key购买 nike

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

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++ 中进行了编译,并且编译正常。我应该注意的 2 个编译器之间是否存在任何差异?导致 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;
}

最佳答案

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

从 C++ 11 n4296 草案开始,在 14.2 Names of template specializations [temp.names] §3 中说:

... Similarly, the first non-nested >> is treated as two consecutive but distinct > tokens, the first of which is taken as the end of the template-argument-list and completes the template-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,Microsoft 声明他们的 C++11/14/17 FeaturesRight angle brackets由 VS2013 和 2015 实现。所以我假设项目属性中的某处应该有一个代码级别指示。

关于c++ - 关于 MSVC 和 g++ 处理 C++ 模板代码的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com