Reputation: 177
I stumbled across this piece of code from ACCU (http://accu.org/index.php/journals/1916) and is interested in applying AOP in C++ by using CRTP for a fun project. The code given by the author is as follows:
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;
};
};
}
I tried compiling it in Microsoft VS2015 and it gave me the following set of errors:
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 ';'
I took the same code, checked the syntax and compiled it in g++ and it compiled fine. Are there any discrepancy between the 2 compilers that I should be aware of? What is the issue here that makes cl.exe generate these errors? Are they due to the fact that how cl.exe parse any template based code? What changes are needed to get this code to work on msvc?
Edit:
Here's the complete code for test.cpp as provided by the author to help you guys get a clearer picture:
#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;
}
Upvotes: 1
Views: 648
Reputation: 177
So after fiddling around and reading whatever I can, I finally sent the original author an email. So all credit goes to him, Mr. Hugo Arregui.
With respect to the line that cl is complaining, the fix is the following:
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> >;
};
It would be great if anyone knows why Microsoft's compiler requires us to expand at least 2 template class in the template argument and give an explanation here. Was talking to the author this and he was surprised as well.
Upvotes: 0
Reputation: 148870
IMHO it is the >>
syndrom on templated templates. Before C++11, a space was required to have to separate >
token and not one >>
token.
Starting from C++ 11 n4296 draft says in 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.
It looks like MSVC2015 has not still implemented that part of standard (or you may have forgotten to declare the C++ version of the source*)
For sake of completeness, when compiling with CLang 3.4.1 without specifying std=c++11
it show this error:
error: a space is required between consecutive right angle brackets (use '> >') using Type = A1<typename Apply<Aspects...>::template Type<T>>; // the er...
and hopefully not even a warning in C++11 mode...
(*) unfortunately, I have no access to a VS2015, by Microsoft declares int their C++11/14/17 Features page that Right angle brackets is implemented by VS2013 and 2015. So I assume there should be somewhere in the project properties a code level indication.
Upvotes: 3