避免打开类型以允许持续折叠

时间:2013-03-03 00:02:55

标签: c++ templates constantfolding

我试图找到一个类层次结构,允许在其上实现处理器寄存器和操作的占位符。它还应该允许常量在运行时折叠。为简单起见,我只看一个操作,这里是乘法。占位符和常量应均匀可访问,即具有共同的基类。

以下代码定义了以下类:

class A:占位符(寄存器)和常量的基类

class B:寄存器的寄存器(其结构包含其名称)

class C:所有常量的基础

class CIint常量

class CFfloat常量

#include <iostream>
#include <memory>
#include <cassert>

class A {
public:
  virtual ~A(){}
};

class B : public A {
};

class C : public A {};

class CI : public C {
public:
  typedef int Type_t;
  int getValue() {return 1;}
};

class CF : public C {
public:
  typedef float Type_t;
  float getValue() {return 1.1;}
};




typedef std::shared_ptr<A> Aptr;
typedef std::shared_ptr<B> Bptr;
typedef std::shared_ptr<C> Cptr;
typedef std::shared_ptr<CI> CIptr;
typedef std::shared_ptr<CF> CFptr;


template<class T, class T2> struct promote {};
template<> struct promote<float,int>   { typedef float Type_t; };
template<> struct promote<float,float> { typedef float Type_t; };
template<> struct promote<int,float>   { typedef float Type_t; };
template<> struct promote<int,int  >   { typedef int   Type_t; };


template<class T1, class T2>
typename promote<typename T1::element_type::Type_t,
         typename T2::element_type::Type_t>::Type_t
mul_const( const T1& c1 , const T2& c2 )
{
  std::cout << c1->getValue() * c2->getValue() << "\n";
  return c1->getValue() * c2->getValue();
}



template<class T>
std::shared_ptr<T> get(const Aptr& pA) {
  return std::dynamic_pointer_cast< T >( pA );
}


Aptr create_A(float f) { return std::make_shared<A>(); }
Aptr create_A(int i)   { return std::make_shared<A>(); }


Aptr mul_const( const Cptr& cp1 , const Cptr& cp2 )
{
  if (auto c1 = get<CI>(cp1))
    if (auto c2 = get<CF>(cp2)) {
      return create_A( mul_const(c1,c2) );
    }
  if (auto c1 = get<CF>(cp1))
    if (auto c2 = get<CI>(cp2)) {
      return create_A( mul_const(c1,c2) );
    }
  if (auto c1 = get<CI>(cp1))
    if (auto c2 = get<CI>(cp2)) {
      return create_A( mul_const(c1,c2) );
    }
  if (auto c1 = get<CF>(cp1))
    if (auto c2 = get<CF>(cp2)) {
      return create_A( mul_const(c1,c2) );
    }
  assert(!"oops");
}



Aptr mul( const Aptr& pA1, const Aptr& pA2 ) 
{
  if (auto c1 = get<C>(pA1))
    if (auto c2 = get<C>(pA2)) 
      {
    return mul_const(c1,c2);
      }
}


int main()
{
  Aptr pA1( new CF );
  Aptr pA2( new CI );

  Aptr result = mul( pA1, pA2 );
}

我对上述代码的问题是函数Aptr mul_const( const Cptr& cp1 , const Cptr& cp2 )。它基本上包含所有可能的常量类型组合的类型切换。它有效,但我想知道这是否可以更优雅地完成?

1 个答案:

答案 0 :(得分:1)

我猜你可以做编译器做的事情,并在浮动时将其他参数转换为float。您可能需要一个新功能来进行转换,并且需要一个&#34; isFloat&#34; (或&#34; isInt&#34;)。我不相信它给你带来了那么多好处,真的......

// Add two virtual member functions here:
class C : public A {
    public:
       virtual bool isInt() = 0;
       virtual float getAsFloat() = 0;
};

然后执行:

class CI : public C {
public:
  typedef int Type_t;
  int getValue() {return 1;}
  float getAsFloat() { return getValue(); }
  bool isInt() { return true; }
};

class CF : public C {
public:
  typedef float Type_t;
  float getValue() {return 1.1;}
  float getAsFloat() { return getValue(); }
  bool isInt() { return false; }
};

现在,您的mul_const变为:

Aptr mul_const( const Cptr& cp1 , const Cptr& cp2 )
{
  if (cp1.isInt() && cp2.isInt())
  {
     CIptr c1 = get<CI>(cp1));
     CIptr c2 = get<CI>(cp2));
     std::cout << c1->getValue() * c2->getValue() << "\n";
     return CIptr(c1->getValue() * c2->getValue());
  }
  else
  {
     std::cout << cp1->getAsFloat() * cp2->getAsFloat() << "\n";
     return CFptr(cp2->getAsFloat() * cp2->getAsFloat());
  }
  // This becomes unreachable... Probably OK to delete.
  assert(!"oops");
}

[我认为可以删除一些模板部分......]