多个级别的嵌套模板.我如何让它工作

Several levels of nested templates. How do I get this to work?

本文关键字:工作 嵌套      更新时间:2023-10-16

我正在做一些模板元编程,我有这样的情况,首先我有几个类,比如:-

template <typename Q>
struct Object {
 public:
  Q data;
};
template <typename P>
class CircleObject : public Object<const typename P::Circle> {
};
template <typename P>
class SquareObject : public Object<const typename P::Circle> {
};
template <typename P>
class Storage {
 public:
  typedef CircleObject<P> MyCircle;
  typedef SquareObject<P> MySquare;
};

现在,我正在尝试定义这些对象的一些特征:-

template <typename P>
struct CircleTraits<Storage<P> > {
  template <typename otype>
  struct IsCircle {
    static const bool VALUE = false;
  };
};
template <typename P>
struct CircleTraits<Storage<P> >::IsCircle<Storage<P>::MyCirlce> {
  static const bool VALUE = true;
};

但是,这是不正确的(编译错误)。我已经尝试了一种将字体名称和模板参数放在任何地方的试错方法,但没有对模板专业化的深刻理解,我真的无法解决这个问题。有人可以在这里帮忙吗?

我希望在以后的函数中实现的是这样的:-

typedef Storage<RedObjects> RedStorage;
template <typename SpecializedStorage>
class Processor {
  typedef CircleTraits<typename SpecializedStorage> MyCircleTraits;
  template <typename ObjectType>
  void foo(ObjectType& data);
};
template <typename SpecializedStorage>
template <typename ObjectType>
void foo(ObjectType& data) {
  if (MyCircleTraits::template IsCircle<ObjectType>::VALUE) {
    // do something about the damn circles
  }
}
我认为

你不能那样做,你可能应该使用 SFINAE 来解决这样的事情:

//C++11 version
template<typename T>
struct IsCircle
{
private:
    template<typename Z>
    constexpr static bool _is(typename Z::MyCirlce*) //if Z dont have `MyCirlce` then this function is removed
    {
        return true;
    }
    template<typename Z>
    constexpr static bool _is(...) //fallback function
    {
        return false;
    }
public:
    static const bool VALUE = _is<T>(nullptr);
};
//C++98 version
template<typename T>
struct IsCircle
{
private:
    struct a { char a; }; //size ~1
    struct b { char a[8]; }; //size ~8
    template<typename Z>
    static b _is(typename Z::MyCirlce*);
    template<typename Z>
    static a _is(...);
public:
    static const bool VALUE = sizeof(_is<T>(0)) == sizeof(b);
};