从函数返回带有值模板参数的类模板
Return a class template with value template arguments from function
假设我有一个简单的模板类:
template <typename ElementType, ElementType Element>
class ConsecutiveMatcher
{
public:
bool operator () (ElementType lhs, ElementType rhs)
{
return lhs == Element && rhs == Element;
}
};
我通常会通过提供一个函数来使实例化比ConsecutiveMatcher<wchar_t, L''>()
更简单,该函数可以根据参数类型推断模板参数类型:
template <typename ElementType>
ConsecutiveMatcher<ElementType, Element /* ?? */>
MakeConsMatcher(ElementType Element)
{
return ConsecutiveMatcher<ElementType, Element>();
}
但是,在这种情况下,MakeConsMatcher(L'')
将不起作用,因为函数需要返回一个类,该类的模板不仅包含类型,还包含值。
如何从既有类型模板参数又有值模板参数的函数返回类模板?
您想将运行时计算的值转换为模板参数吗?这是不可能的。
我只是在寻找一种方法来省略wchar_t,并在实例化过程中使用自动类型推导。
我可以想象这样的情况:
-
只有在运行时才知道的参数类型(您对此一无所知):您无法使用模板处理它:您将希望重新设计代码并使用继承和虚拟函数(或者,可能将模板和继承混合使用)
-
编译时已知参数类型,运行时已知参数值:在模板参数列表中留下参数的类型,并将参数值传递给构造函数,然后为了用户方便,使工厂函数推断类型
template<typename T> struct MyType { template <class T> MyType(const T& defaultValue) : value(defaultValue) {} T value; }; template<typename T> MyType<T> MyFactory(const T& defaultValue) { return MyType<T>(defaultValue); } int main() { char c = 'a'; wchar_t w = L'a'; int i = 42; float f = 3.14f; auto mt_char = MyFactory(c); auto mt_wchar = MyFactory(w); auto mt_int = MyFactory(i); auto mt_float = MyFactory(f); }
-
在编译时,您知道一个类型列表,并希望它们的行为不同(例如,具有不同的默认值):为列表中的每个类型进行模板专门化,然后,为了用户方便,创建typedefs
template<typename T> struct MyType { MyType(const T& defaultValue) : value(defaultValue) {} T value; }; template<> struct MyType <char> { MyType() : value('c') {} char value; }; template<> struct MyType <wchar_t> { MyType() : value(L'w') {} wchar_t value; }; typedef MyType<char> MyTypeChar; typedef MyType<wchar_t> MyTypeWchar; int main() { MyTypeChar mt_char_default; MyTypeWchar mt_wchar_default; }
在这种情况下,用户仍然可以实例化自己的专业化。这种方法的例子是std::basic_string
类。此外,如果使类成员static
或static const
,并且对于积分类型,只需在成员列表中定义即可,则可以简化您的专业化:
template<>
struct MyType <char>
{
static const char value = 'c';
};
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类