Вопрос

I'm doing some template meta programming and I have a situation like this, first I have a few classes like :-

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;
};

Now, I'm trying to define some traits of these objects as such :-

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;
};

However, this is incorrect (compile errors). I've tried a trial and error method of putting typenames and template parameters everywhere but without a firm understanding of template specializations, I'm not really able to fix this. Can someone help here?

What I'm hoping to achieve in a later function is something like :-

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
  }
}
Это было полезно?

Решение

I think you cant do it like that, You probably should use SFINAE to solve something like this:

//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);
};
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top