从模板类的另一个实例创建模板类的实例时省略模板参数

从模板类的另一个实例创建模板类的实例时省略模板参数

问题描述:

在创建类 DeriveGenerator< T3,T4,T1,T2> T1,T2 / code>来安慰我的生活。

I want to omit some template parameter T1,T2 when create an instance of a class DeriveGenerator<T3,T4,T1,T2> to comfort my life.

这是我遇到的最终简化版本。

Here is the ultimately simplified version of what I am encountering.

我的图书馆:-

重要的部分是类声明。 (这行

它们的内部内容只是填充物。

The important part is the class declaration. (this line)
Their internal content is just a filler.

template<class T1,class T2>class BaseGenerator{ //<-- this line
    public: std::pair<T1*,T2*> generateBase(){ 
        /** actually create T3,T4 internally */
        return std::pair<T1*,T2*>(nullptr,nullptr);
    }
};
template<class T3,class T4,class T1,class T2>class DeriveGenerator{ //<-- this line
    public: Base<T1,T2>* base;
    public: std::pair<T3*,T4*> generateDerive(){ 
        auto pp=base->generateBase();
        return std::pair<T3*,T4*>((T3*)(pp.first),(T4*)(pp.second));
    }
};

用户:-

class B1{};class B2{};
class B3:public B1{};
class B4:public B2{};
int main() {
    //v this is what I have to
    BaseGenerator<B1,B2> baseGen;
    DeriveGenerator<B3,B4,B1,B2> deriveGen;  //so dirty #1
    deriveGen.base=&baseGen;
    deriveGen.generateDerive();
}



问题



是否可以使#1 行更清洁?

我想要 deriveGen 的类型取决于 baseGen 的类型。

Question

Is it possible to make the line #1 cleaner?
I want the type of deriveGen depends on the type of baseGen.

这就是我想要的:-

BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4> deriveGen;   //<-- modified
deriveGen.base=&baseGen;

或至少类似以下内容:-

or at least something like:-

BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4, DECLARATION_TYPE(baseGen) > deriveGen;   //<-- modified
deriveGen.base=&baseGen;

我读过(仍然不知道):-

I have read (still no clue):-

  • Omitting arguments in C++ Templates
  • Skipping a C++ template parameter
  • Difference when omitting the C++ template argument list

我什至不知道是否有可能。

I don't even know if it is possible.

decltype 似乎是最接近的线索,但是我找不到将其应用于这种情况的方法。

我想我可能必须将其拆分为 T1,T2 ....(?)

"decltype" seems to be the closest clue, but I can't find a way to apply it to this case.
I think I may have to split it to T1,T2.... (?)

实际上, baseGen 是某些字段的非静态字段尚未实例化的类,例如

In real case, baseGen is a non-static field of some classes that is not instantiated yet e.g.

class Holder{
    public: BaseGenerator<B1,B2> baseGen;
};

因此,在声明 deriveGen 时,我无法达到 baseGen 的真实实例。

这是困难的部分。

Therefore, at the time of declaring deriveGen, I can't reach the real instance of baseGen.
That is the hard part.

不过,我可以通过 decltype 引用 baseGen 的类型。

(对不起

I can refer baseGen's type via decltype, though.
(sorry for not mention about it)

不确定要了解什么,但是...我想您可以定义 DeriveGenerator

Not sure to understand what you want but... I suppose you can define DeriveGenerator in this way

template <typename, typename, typename>
class DeriveGenerator;

template <typename T3, typename T4, typename T1, typename T2>
class DeriveGenerator<T3, T4, BaseGenerator<T1, T2>>
 {
   public:
      BaseGenerator<T1,T2>* base;

   public:
      std::pair<T3*,T4*> generateDerive ()
       { 
         auto pp=base->generateBase();
         return std::pair<T3*,T4*>((T3*)(pp.first),(T4*)(pp.second));
       }
 };

并按以下方式使用

BaseGenerator<B1,B2> baseGen;
DeriveGenerator<B3,B4,decltype(baseGen)> deriveGen; 

如果您对 T1 感兴趣和 T2 类型;如果您只对 BaseGenerator< T1,T2> 感兴趣,则可以简单地写

This if you're interested in T1 and T2 types; if you're only interested in BaseGenerator<T1, T2> you can simply write

template <typename T3, typename T4, typename Tbase>
class DeriveGenerator
 {
   public:
      Tbase * base;

   public:
      std::pair<T3*,T4*> generateDerive ()
       { 
         auto pp=base->generateBase();
         return std::pair<T3*,T4*>((T3*)(pp.first),(T4*)(pp.second));
       }
 };