Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我可以在宏中编写模板类吗?_C++_Templates_Macros - Fatal编程技术网

C++ 我可以在宏中编写模板类吗?

C++ 我可以在宏中编写模板类吗?,c++,templates,macros,C++,Templates,Macros,我有一个模板类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 *cr

我有一个模板类
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_orginal
{
  #define T file1_orginal
   A()
   anotherMacro1(passingValue);
   anotherMacro2(passingValue);
  #endif

   .....other data member vs member function
}
A也是一个宏

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

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

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

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

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

#define T file1
A<T>
#undef T
#定义T文件1
A.
#未定义T
与:

A<file1>
A
然而,你肯定让整件事变得不那么可读了。重点是什么?这似乎是一个解决问题的办法。我无法想象这是解决问题的办法


好的,鉴于你的宏示例,我建议逐步重构模板。在宏中使用“受保护”可见性是值得怀疑的,但我们会保留它。首先,我要说:

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?
};
template//奇怪的重复模板模式
甲级{
受保护的:
INTA;
int b;
静态CRTP*ptr;
公众:
空集_a(int aa){a=aa;}
静态D*create(){return new CRTP();}//D是基类吗?
};
然后:

class file1 : public A<file1> {
    #define T file1
        anotherMacro1(passingValue);
        anotherMacro2(passingValue);
    #undef T
    ...
};
类文件1:公共A{
#定义T文件1
anotherMacro1(传递值);
anotherMacro2(传递值);
#未定义T
...
};

这使用来从A中排除成员。我从未尝试过在基类中使用受保护的成员,但我似乎记得受保护的成员是继承的,并作为受保护的成员进行后续继承。我会考虑对这些受保护成员的设计要求很差,并将它们重构为Access功能,但我认为这是你想要的。稍后您可以重构anotherMacro1和anotherMacro2的内容。

虚拟静态
函数?类,该类称为“使用”<代码>#未定义是否没有宏名称?是C++吗?10类之一被称为“使用”<代码>使用< /C> >是C++关键字!!这尤其不会编译…为什么不直接将类作为模板参数传递?我不知道你想要达到什么。除了<>代码>使用< /C> >是一个C++关键字,你是否试图做<代码> A<代码>?这个问题似乎是偏离主题的,因为它是对语言缺乏理解的。谢谢你的回答。实际上,我正在尝试删除源代码中的所有宏。但是嵌套的宏太多了。我知道A工作得很好,但#定义A \另一个#宏#未定义。在“另一个宏”中,我也需要“T”。所以我应该一步一步地删除宏。我很难理解你的意思。您是否可以编辑您的问题,使其包含一些原始宏,以说明您试图替换的多层宏?这可能有助于澄清您正在解决的问题。我已经添加了原始宏。你能帮我一下吗?看看你对anotherMacro1的定义,我鼓励你阅读关于std::bind和std::function(如果你没有C++11,也可以阅读Boost实现)。使用函数对象可能会消除anotherMacro1正在创建的静态转发函数的需要。
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
    ...
};