我试图实现一个模板,将有以下定义
template<typename TypeToConvert, typename ... ListOfAvailableTypes>
struct best_match{...}
并且将屈服于提供2个成员的类型
1. value_type typedef - that is either exact match of TypeToConvert, if TypeToConvert was listed in ListOfAvailableTypes pack, or is the best suited type to convert TypeToConvert to, from the list of types available in ListOfAvailableTypes
2. int index - that will contain the position of the found type in ListOfAvailableTypes pack
举个小例子
best_match<const char[4], std::string, int, void const*>
应屈服于提供少于2个成员的类型
best_match<const char[4], std::string, int, void const*>::value_type = void const*
best_match<const char[4], std::string, void const*>::index = 2
如果我想找到精确匹配或可转换匹配,我可以实现这一点,但我如何才能找到最适合的可转换类型呢?
2条答案
按热度按时间hrirmatl1#
您可以利用重载解析。可能有比这更短的实现
laik7k3q2#
如何使用C++20