C++ 如何在编译时注册类型的键值对?
比如说我有一个班级测试和一个班级测试。我不想让Impl成为测试的内部类。我如何在编译时创建一些全局键值(Impl类型)存储库,在其中可以使用该类型提取Impl类型 比如:C++ 如何在编译时注册类型的键值对?,c++,C++,比如说我有一个班级测试和一个班级测试。我不想让Impl成为测试的内部类。我如何在编译时创建一些全局键值(Impl类型)存储库,在其中可以使用该类型提取Impl类型 比如: Impl<Test>::Type Impl::Type 这将导致TestImpl。定义一个没有实现的通用模板,然后对其进行专门化: template <typename T> struct Impl; template<> struct Impl<Test> { typede
Impl<Test>::Type
Impl::Type
这将导致TestImpl。定义一个没有实现的通用模板,然后对其进行专门化:
template <typename T> struct Impl;
template<> struct Impl<Test> { typedef TestImpl Type; };
模板结构Impl;
模板结构Impl{typedef TestImpl Type;};
依此类推,对于您需要的每种类型。定义一个没有实现的通用模板,然后对其进行专门化:
template <typename T> struct Impl;
template<> struct Impl<Test> { typedef TestImpl Type; };
模板结构Impl;
模板结构Impl{typedef TestImpl Type;};
依此类推,对于您需要的每种类型。定义一个没有实现的通用模板,然后对其进行专门化:
template <typename T> struct Impl;
template<> struct Impl<Test> { typedef TestImpl Type; };
模板结构Impl;
模板结构Impl{typedef TestImpl Type;};
依此类推,对于您需要的每种类型。定义一个没有实现的通用模板,然后对其进行专门化:
template <typename T> struct Impl;
template<> struct Impl<Test> { typedef TestImpl Type; };
模板结构Impl;
模板结构Impl{typedef TestImpl Type;};
依此类推,对于您需要的每种类型。组织接口和实现的一种方便方法是使用关联它们的标记:
template<typename TAG> class interface;
template<typename TAG> class impl;
template<typename> struct impl_of;
template<typename TAG>
struct impl_of<interface<TAG> > { using type = impl<TAG>; };
这种情况下所需的映射由的impl\u提供,它只定义了一次
这听起来可能太多了,但如果您要定义许多类(如interface
,impl
)和它们的许多特性(如impl\u of
),它会非常强大。组织接口和实现的一种方便方法是使用一个将它们关联起来的标记:
template<typename TAG> class interface;
template<typename TAG> class impl;
template<typename> struct impl_of;
template<typename TAG>
struct impl_of<interface<TAG> > { using type = impl<TAG>; };
这种情况下所需的映射由
的impl\u提供,它只定义了一次
这听起来可能太多了,但如果您要定义许多类(如interface
,impl
)和它们的许多特性(如impl\u of
),它会非常强大。组织接口和实现的一种方便方法是使用一个将它们关联起来的标记:
template<typename TAG> class interface;
template<typename TAG> class impl;
template<typename> struct impl_of;
template<typename TAG>
struct impl_of<interface<TAG> > { using type = impl<TAG>; };
这种情况下所需的映射由
的impl\u提供,它只定义了一次
这听起来可能太多了,但如果您要定义许多类(如interface
,impl
)和它们的许多特性(如impl\u of
),它会非常强大。组织接口和实现的一种方便方法是使用一个将它们关联起来的标记:
template<typename TAG> class interface;
template<typename TAG> class impl;
template<typename> struct impl_of;
template<typename TAG>
struct impl_of<interface<TAG> > { using type = impl<TAG>; };
这种情况下所需的映射由
的impl\u提供,它只定义了一次
这听起来可能太多了,但如果要定义许多类(如interface
,impl
)和它们的许多特性(如impl\u of
),它会非常强大。这似乎是一种可以用普通预处理器宏处理的事情。有什么原因不起作用吗?这似乎是一个普通的预处理器宏可以处理的事情。有什么原因不起作用吗?这似乎是一个普通的预处理器宏可以处理的事情。有什么原因不起作用吗?这似乎是一个普通的预处理器宏可以处理的事情。有什么理由不起作用吗?