有没有办法决定模板 class 的模板参数类型?
Is there a way to decide template argument types of template class?
我有两个模板class这些参数是在软件的不同层中决定的。
我必须在下层使用的 class 是
template <class RoutineInfoId,
class ErrorInfoId,
class LoadBarInfoId>
class InformCBSet
我要在上层使用的class是
template <class LanguageId,
class RoutineInfoId,
class ErrorInfoId,
class LoadBarInfoId>
class Info
我也可以创建 'Info' class
template <class LanguageId,
class SomeInformCBSet>
而
typedef InformCBSet<SomeRoutineInfoId,
SomeErrorInfoId,
SomeLoadBarInfoId> SomeInformCBSet
我想通过直接使用SomeInfoCBSet作为'Infoclass的模板参数从上层的SomeInfoCBSet中获取(即下层class的类型)。
有什么办法可以实现吗?
谢谢
您可以使用这个或类似的东西:
template<typename T> struct InformCBSetTypes;
template<typename T0, typename T1, typename T2>
struct InformCBSetTypes<InformCBSet<T0, T1, T2> > {
typedef T0 RoutineInfoId;
typedef T1 ErrorInfoId;
typedef T2 LoadBarInfoId;
};
能够在您的 Info
class 模板中使用 typename InformCBSetTypes<SomeInformCBSet>::RoutineInfoId
等。出于演示目的:
#include <iostream>
#include <string>
template<typename T0, typename T1, typename T2>
struct InformCBSet { };
template<typename T> struct InformCBSetTypes;
template<typename T0, typename T1, typename T2>
struct InformCBSetTypes<InformCBSet<T0, T1, T2> > {
typedef T0 RoutineInfoId;
typedef T1 ErrorInfoId;
typedef T2 LoadBarInfoId;
};
int main() {
typedef InformCBSet<int, double, std::string> MySet;
// Note: To use these inside a template, you'll have to write "typename"
// before them (as with all typedefs in dependent types) so that the
// compiler knows to expect a type before it knows which specialization
// of InformCBSetTypes it's ultimately going to use.
InformCBSetTypes<MySet>::RoutineInfoId i = 1; // int
InformCBSetTypes<MySet>::ErrorInfoId d = 2.3; // double
InformCBSetTypes<MySet>::LoadBarInfoId s = "Hello, world!"; // string
std::cout << i << ", " << d << ", " << s << std::endl;
}
您可以使用另一个模板作为助手来生成所需的类型:
// Helper accepts two types: a language ID and an InformCBSet instantiation. This
// declares the base template, but we need to specialize it, so there is no base
// template implementation.
template <typename, typename>
struct InformCBSet_to_Info;
// Specialization where the second argument is an InformCBSet instantiation.
template <typename LanguageId,
typename RoutineInfoId,
typename ErrorInfoId,
typename LoadBarInfoId>
struct InformCBSet_to_Info<LanguageId, InformCBSet<RoutineInfoId,
ErrorInfoId,
LoadBarInfoId>>
{
typedef Info<LanguageId,
RoutineInfoId,
ErrorInfoId,
LoadBarInfoId> info_type;
};
typedef InformCBSet<SomeRoutineInfoId,
SomeErrorInfoId,
SomeLoadBarInfoId> SomeInformCBSet;
typedef InformCBSet_to_Info<SomeLanguageId, SomeInformCBSet>::info_type SomeInfoType;
我有两个模板class这些参数是在软件的不同层中决定的。 我必须在下层使用的 class 是
template <class RoutineInfoId,
class ErrorInfoId,
class LoadBarInfoId>
class InformCBSet
我要在上层使用的class是
template <class LanguageId,
class RoutineInfoId,
class ErrorInfoId,
class LoadBarInfoId>
class Info
我也可以创建 'Info' class
template <class LanguageId,
class SomeInformCBSet>
而
typedef InformCBSet<SomeRoutineInfoId,
SomeErrorInfoId,
SomeLoadBarInfoId> SomeInformCBSet
我想通过直接使用SomeInfoCBSet作为'Infoclass的模板参数从上层的SomeInfoCBSet中获取(即下层class的类型)。
有什么办法可以实现吗? 谢谢
您可以使用这个或类似的东西:
template<typename T> struct InformCBSetTypes;
template<typename T0, typename T1, typename T2>
struct InformCBSetTypes<InformCBSet<T0, T1, T2> > {
typedef T0 RoutineInfoId;
typedef T1 ErrorInfoId;
typedef T2 LoadBarInfoId;
};
能够在您的 Info
class 模板中使用 typename InformCBSetTypes<SomeInformCBSet>::RoutineInfoId
等。出于演示目的:
#include <iostream>
#include <string>
template<typename T0, typename T1, typename T2>
struct InformCBSet { };
template<typename T> struct InformCBSetTypes;
template<typename T0, typename T1, typename T2>
struct InformCBSetTypes<InformCBSet<T0, T1, T2> > {
typedef T0 RoutineInfoId;
typedef T1 ErrorInfoId;
typedef T2 LoadBarInfoId;
};
int main() {
typedef InformCBSet<int, double, std::string> MySet;
// Note: To use these inside a template, you'll have to write "typename"
// before them (as with all typedefs in dependent types) so that the
// compiler knows to expect a type before it knows which specialization
// of InformCBSetTypes it's ultimately going to use.
InformCBSetTypes<MySet>::RoutineInfoId i = 1; // int
InformCBSetTypes<MySet>::ErrorInfoId d = 2.3; // double
InformCBSetTypes<MySet>::LoadBarInfoId s = "Hello, world!"; // string
std::cout << i << ", " << d << ", " << s << std::endl;
}
您可以使用另一个模板作为助手来生成所需的类型:
// Helper accepts two types: a language ID and an InformCBSet instantiation. This
// declares the base template, but we need to specialize it, so there is no base
// template implementation.
template <typename, typename>
struct InformCBSet_to_Info;
// Specialization where the second argument is an InformCBSet instantiation.
template <typename LanguageId,
typename RoutineInfoId,
typename ErrorInfoId,
typename LoadBarInfoId>
struct InformCBSet_to_Info<LanguageId, InformCBSet<RoutineInfoId,
ErrorInfoId,
LoadBarInfoId>>
{
typedef Info<LanguageId,
RoutineInfoId,
ErrorInfoId,
LoadBarInfoId> info_type;
};
typedef InformCBSet<SomeRoutineInfoId,
SomeErrorInfoId,
SomeLoadBarInfoId> SomeInformCBSet;
typedef InformCBSet_to_Info<SomeLanguageId, SomeInformCBSet>::info_type SomeInfoType;