【问题标题】:Could I write a template class within a macro?我可以在宏中编写模板类吗?
【发布时间】:2014-01-13 19:18:58
【问题描述】:

我有一个模板类A,我会知道T 取决于哪个类调用它。

例如,有 10 个类将使用类 A,10 个类中的一个称为file1

我可以编写file1类中所示的代码吗?

class D;

template<typename T>
class A
{
  protected:
      int a;
      int b;
      static T *ptr;
  public:
      static void set_a(int aa){ a = aa; }
      static D *create()
        { return new T(); }
};

我可以做如下的事情

 class file1
 {
    #define T file1   
     A<T>   
    #undef

     .....other data member vs member function
 }


 class file2
 {
    #define T file2   
     A<T>      
    #undef

     .....other data member vs member function
 }

原始代码如下所示:

另一个宏1在哪里

#define anotherMacro1(method)\
  public:\
  static return_type functionname(passingtype *ptr1, passingtype *ptr2)\
  {\
  return ((T *)ptr1)->method(ptr2);\
  }

================================================ ========================

A也是一个宏

喜欢

 #define A \
  protected:\
      int a;\
      int b;\
      static T *ptr;\
  public:\
      static void set_a(int aa){ a = aa; }\
      static D *create()\
        { return new T(); }

================================================ ================================

   class file1_orginal
{
  #define T file1_orginal
   A()
   anotherMacro1(passingValue);
   anotherMacro2(passingValue);
  #endif

   .....other data member vs member function
}

首先我想删除A中的宏,所以我用A类来替换宏A。

【问题讨论】:

  • virtual static 函数?哪个类被称为“使用”? #undef 没有宏名称?是 C++ 吗?
  • '10 个类中的一个被称为“使用”' using 是一个 c++ 关键字!!那特别不会编译...
  • 为什么不直接将类作为模板参数传递呢?我真的没有得到你想要达到的目标。
  • 除了 using 是一个 c++ 关键字之外,您是否尝试这样做 A&lt;using&gt;
  • 这个问题似乎是题外话,因为它是关于缺乏对语言的最低限度的理解。

标签: c++ templates macros


【解决方案1】:

严格来说,是的,您可以这样做。实际上,我不确定我是否在以下方面看到任何价值:

#define T file1
A<T>
#undef T

对比:

A<file1>

但是,您肯定使整个内容的可读性降低。重点是什么?这似乎是寻找问题的解决方案。我无法想象这是解决方案的问题。


好的,鉴于您的示例宏,我建议逐步重构为模板。在宏中使用“受保护”可见性是有问题的,但我们会保留它。我会从这个开始:

template<class CRTP>    //Curiously Recurring Template Pattern
class A {
    protected:
        int a;
        int b;
        static CRTP *ptr;
    public:
        void set_a(int aa){ a = aa; }
        static D *create() { return new CRTP(); }  //D is a base class?
};

然后:

class file1 : public A<file1> {
    #define T file1
        anotherMacro1(passingValue);
        anotherMacro2(passingValue);
    #undef T
    ...
};

这使用curiously recurring template pattern 来嵌入来自A 的成员。我从未尝试过对基类中的受保护成员进行此操作,但我似乎记得受保护的成员是继承的,并作为受保护的成员继承以供将来继承。无论如何,我认为这些受保护成员的需​​求设计不佳,并将它们重构为访问器功能,但我认为这可以满足您的需求。稍后您可以重构 anotherMacro1 和 anotherMacro2 的内容。

【讨论】:

  • 感谢您的回复。实际上我正在尝试删除源代码中的所有宏。但是嵌套宏太多了。我知道 A 运行良好,但 #define A \ another_macro\#undef.在“another_macro”中,我也需要“T”。所以我应该逐步删除宏。
  • 我很难理解您的意思。您可以编辑您的问题以包含一些原始宏来说明您尝试替换的多层宏吗?这可能有助于澄清您正在解决的问题。
  • 我已经添加了原始宏。你能帮我解决一下吗?
  • 查看您对 anotherMacro1 的定义,我鼓励您阅读有关 std::bind 和 std::function 的信息(或者如果您没有 C++11,则为 Boost 实现)。使用函数对象可能会消除对 anotherMacro1 正在创建的静态转发函数的需要。
猜你喜欢
  • 1970-01-01
  • 2019-08-15
  • 2023-03-29
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 2011-05-11
  • 2013-08-01
  • 1970-01-01
相关资源
最近更新 更多