C++ Template: typenames and ||s in the angle brackets

Viewed 62

I'm browsing this piece of code right here, the TensorRT Engine serialization and deserialization source code. I stumbled across some syntax I've never seen before.

struct Serializer<T, // ??
    typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value || std::is_pod<T>::value>::type>
{ // ...

What is the code after the commented question marks doing? There is no narrower question, this syntax is completely foreign to me. I'd be greatly appreciative of any resources I can read to learn more about what this is doing exactly.

I usually present a list of google queries I've performed before asking questions, but I'm not even sure what to call this pattern.

Source Code: https://github.com/NVIDIA/TensorRT/blob/492878b2df3c9bf19a26c2b66cba129f450475bc/plugin/common/serialize.hpp#L45

1 Answers

The syntax evaluates to void (to match the presumable default template argument) or fails to do so. You may need to review C++ templates again. SFINAE is a technique used to constrain template specializations via that failure. Since C++20 concepts provide a much better mechanism.

Related