C ++使用来自不同模板类的枚举作为函数参数

时间:2022-09-07 07:54:20

I got two template classes CarOwner and Truck.

我有两个模板类CarOwner和Truck。

// CarOwner.h
#include "Truck.h"
template<size_t T1, typename T2>
class CarOwner {
public:
    enum MyEnum {
        red = 0,
        green
    }

   void DoSomething();

private:
   Truck<DataContainer<T1,T2>> truck_;   
   MyEnum color;
}

// CarOwner.hpp
template<size_t T1, typename T2>
void CarOwner<T1,T2>::DoSomething(){
    this->truck_.setEnum(this->color);
}

// Truck.h
template<typename G>
class Truck {
    void setEnum(CarOwner<T1,T2>::MyEnum color); // <---
}

My problem is to understand how to write the function declaration of void setEnum();. As shown in the code above I actually want to pass the function an enum of type CarOwner<T1,T2>::MyEnum color. As I need to #include "Truck.h" in class CarOwner, I can't include the CarOwner in the Truck class. Furthermore, the template parameters T1 and T2 are unknown inside class Truck as it has a different template type G.

我的问题是要了解如何编写void setEnum();的函数声明。如上面的代码所示,我实际上想要传递一个类型CarOwner :: MyEnum颜色的枚举函数。因为我需要在CarOwner类中#include“Truck.h”,所以我不能在Car类中包含CarOwner。此外,模板参数T1和T2在类Truck中是未知的,因为它具有不同的模板类型G. ,t2>

I have no clue how to properly declare this function to accept the CarOwner<T1,T2>::MyEnum. Any help is appreciated!

我不知道如何正确地声明这个函数来接受CarOwner :: MyEnum。任何帮助表示赞赏! ,t2>

Edit: template parameter G is a combination of T1 and T2.

编辑:模板参数G是T1和T2的组合。

This code is just an example to state my problem and the design obviously is odd.

这段代码只是说明我的问题的一个例子,设计显然很奇怪。

1 个解决方案

#1


0  

assuming Truck is instantiated as Truck<DataContainer<T1,T2>>, you can write a partial specialization:

假设Truck被实例化为Truck >,您可以编写部分特化:

template<typename G>
class Truck {
    // whatever
};

template<typename T1,typename T2>
class CarOwner;

template<typename T1, typename T2>
class Truck<DataContainer<T1,T2>> {
    void setEnum( typename CarOwner<T1,T2>::MyEnum color );
};

alternatively, you could templatize setEnum over, say, OwnerType:

或者,你可以将setEnum模板化,比如OwnerType:

template<typename G>
class Truck {
  template<typename OwnerType>
  void setEnum( typename OwnerType::MyEnum color);
};

// ...

void CarOwner<T1,T2>::DoSomething(){
  this->truck_.template setEnum<CarOwner<T1,T2>>(this->color);
}

or ...

#1


0  

assuming Truck is instantiated as Truck<DataContainer<T1,T2>>, you can write a partial specialization:

假设Truck被实例化为Truck >,您可以编写部分特化:

template<typename G>
class Truck {
    // whatever
};

template<typename T1,typename T2>
class CarOwner;

template<typename T1, typename T2>
class Truck<DataContainer<T1,T2>> {
    void setEnum( typename CarOwner<T1,T2>::MyEnum color );
};

alternatively, you could templatize setEnum over, say, OwnerType:

或者,你可以将setEnum模板化,比如OwnerType:

template<typename G>
class Truck {
  template<typename OwnerType>
  void setEnum( typename OwnerType::MyEnum color);
};

// ...

void CarOwner<T1,T2>::DoSomething(){
  this->truck_.template setEnum<CarOwner<T1,T2>>(this->color);
}

or ...