联合类型的模板专业化

Template specialization for union type

本文关键字:专业化 类型      更新时间:2023-10-16

我如何专业化union类型的模板?假设我有模板功能

template <typename T>
void foo(T value);

如果T不是任何union类型,我想禁止调用此功能。我该如何实现?

如果T不是任何联合类型,我想禁止调用此功能。我该如何实现?

也许使用std::is_union

template <typename T>
std::enable_if_t<std::is_union<T>::value> foo(T value)
 { /* ... */ }

您可以将std::enable_if(std::enable_if_t(与std::is_union中的CC_7一起使用。类似:

template <class T,
   typename std::enable_if_t<std::is_union<T>::value,
   int> = 0>
   void foo(T t) {
   // an implementation for union types
}

这是Sfinae规则的解释。