A C++ serialize(T&& value) checks std::is_enum_v<T> and misses enums passed as lvalues; why, and should you use std::decay_t or std::remove_cvref_t?
answer
- T deduced as a reference
- a reference is not an enum
- strip before you classify
- decay also turns arrays into pointers
basics
~20 sFor an lvalue argument a forwarding reference deduces T as Kind& or const Kind&, and is_enum_v of a reference type is false. Strip it first: remove_cvref_t (C++20) removes only reference and cv; decay_t also turns arrays and functions into pointers.
solid answer
~40 sWith `template <class T> void serialize(T&& value)`, an lvalue `Kind k` deduces `T = Kind&` and a `const Kind` deduces `const Kind&`; only an rvalue gives plain `Kind`. Primary type categories classify the exact type, and a reference type is not an enumeration type, so `std::is_enum_v<Kind&>` is `false`. Classify `std::remove_cvref_t<T>` (C++20; before that, `std::remove_cv_t<std::remove_reference_t<T>>`). `std::decay_t<T>` also works for enums, but it models **pass-by-value**: besides stripping references and cv it turns `const char(&)[6]` into `const char*` and a function type into a function pointer. Use `remove_cvref_t` when you want to ask about the type the caller has; use `decay_t` when you will store a copy, as `std::make_pair` does through `unwrap_ref_decay_t`. For choosing a wire type, `std::conditional_t` selects between types, but both must be valid, so select traits and read `::type` lazily.
code
cpp · 27 lines#include <cstdint>
#include <iostream>
#include <type_traits>
enum class Kind : std::uint8_t { ping = 1 };
template <class T>
void inspect(T&& value) { // forwarding reference: T may be Kind&
std::cout << std::is_enum_v<T> << std::is_enum_v<std::remove_cvref_t<T>> << ' ';
(void)value;
}
// Pick a wire type lazily: select a trait, then read ::type.
template <class T>
using wire_t = typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>,
std::type_identity<T>>::type;
int main() {
Kind k = Kind::ping;
const Kind ck = k;
inspect(k); inspect(ck); inspect(Kind::ping); // 01 01 11
std::cout << '\n';
static_assert(std::is_same_v<std::decay_t<const char (&)[6]>, const char*>);
static_assert(std::is_same_v<std::remove_cvref_t<const char (&)[6]>, char[6]>);
static_assert(std::is_same_v<std::decay_t<int(double)>, int (*)(double)>);
static_assert(std::is_same_v<wire_t<Kind>, std::uint8_t> && std::is_same_v<wire_t<int>, int>);
}go deeper
Remember that const, volatile and references are part of a type, so a trait asked about const Kind& is not asked about Kind.
Explain what a forwarding reference deduces for lvalues, what remove_cvref_t and decay_t each strip, and how std::conditional selects a type.
Catch traits applied to unstripped forwarding-reference parameters in review, choose decay only for stored copies, and make conditional lazy where a branch may not exist.
Standardise one normalising alias for a codebase's generic code, so that every trait check agrees on what type is being asked about.
## The bug A serializer takes its argument by forwarding reference so it can accept anything without copying: ```cpp template <class T> void serialize(T&& value) { if constexpr (std::is_enum_v<T>) { /* write the underlying integer */ } else { /* ... */ } } Kind k = Kind::ping; serialize(k); // T = Kind& -> is_enum_v<T> is false serialize(Kind::ping); // T = Kind -> is_enum_v<T> is true ``` The deduction rule for `T&&` (covered with forwarding references) makes `T` an **lvalue reference type** when the argument is an lvalue. The primary category traits answer for the exact type they are given, and `Kind&` is a reference type, not an enumeration type. The enum path silently never runs for named variables. ## Stripping the type before you classify it | Trait | Removes | Also does | `const Kind&` | `const char(&)[6]` | `int(double)` | |---|---|---|---|---|---| | `std::remove_reference_t` | reference | nothing | `const Kind` | `const char[6]` | `int(double)` | | `std::remove_cv_t` | top-level `const`/`volatile` | on arrays, the element's cv | `const Kind&` (unchanged) | unchanged | unchanged | | `std::remove_cvref_t` (C++20) | reference, then cv | nothing else | `Kind` | `char[6]` | `int(double)` | | `std::decay_t` | reference, then cv | array to pointer, function to pointer | `Kind` | `const char*` | `int(*)(double)` | `std::remove_cvref_t<T>` is exactly "the type the caller has, without reference or qualifiers". It arrived in C++20; C++17 code writes `std::remove_cv_t<std::remove_reference_t<T>>`. ## What decay models The standard defines `std::decay<T>` as: let `U` be `remove_reference_t<T>`; if `U` is an array, the result is a pointer to its element type; if `U` is a function, the result is a pointer to it; otherwise it is `remove_cv_t<U>`. Those are precisely the adjustments that happen when an argument is **passed by value**. That makes `decay_t` the right tool when you are about to **store a copy**: - `std::make_pair` computes its element types with `std::unwrap_ref_decay_t` (C++20), which is `decay_t` plus unwrapping `std::reference_wrapper`; - a type-erased callback stores `std::decay_t<F>` so that passing a function name stores a function pointer. For **classification**, decay can mislead. A serializer that treats C arrays specially will never see one after `decay_t`, because `const char(&)[6]` has become `const char*`, which `std::is_pointer_v` then reports as a pointer. ## std::conditional and its eager arguments To pick the integer type written on the wire, `std::conditional_t<B, T, F>` is `T` when `B` is true and `F` otherwise. Its arguments are ordinary template arguments, so **both are formed** before `conditional` chooses: ```cpp template <class T> using wire_eager = std::conditional_t<std::is_enum_v<T>, std::underlying_type_t<T>, T>; // wire_eager<int>: error, underlying_type_t<int> cannot be formed ``` Apple clang 21 with libc++ reports `only enumeration types have underlying types`. The lazy fix selects a **trait** and only then asks for its `::type`: ```cpp template <class T> using wire_t = typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>, std::type_identity<T>>::type; ``` `std::underlying_type<int>` as a class is fine to name; only its missing `type` would be an error, and it is never asked for. `std::type_identity` is C++20. ## Pitfalls of the stripping traits - **Top-level only.** `std::remove_cv_t<const int*>` is still `const int*`: the pointer itself is not const, the pointee is. Use `std::remove_pointer_t` first if you mean the pointee. - **Order matters.** `std::remove_cv_t<const int&>` is unchanged, because a reference type has no top-level cv; remove the reference first, which is what `remove_cvref_t` does. - **Arrays keep their extent with `remove_cvref_t`.** `std::remove_cvref_t<const char(&)[6]>` is `char[6]`: removing cv from an array type removes it from the element type. - **`std::is_same_v<T, std::string>` on an unstripped `T`** fails for `std::string&` and `const std::string&`, the two most common ways a string arrives. ## Choosing 1. **Classifying a deduced type**: `std::remove_cvref_t<T>`. 2. **Storing a copy**: `std::decay_t<T>`. 3. **Comparing with a known type**: `std::is_same_v<std::remove_cvref_t<T>, std::string>`, never `std::is_same_v<T, std::string>` on a forwarding-reference `T`. 4. **Choosing between types**: `std::conditional_t`, made lazy whenever one branch might not exist. Write the stripping once, in a helper such as `template <class T> using value_t = std::remove_cvref_t<T>;`, so that every check in the serializer agrees on what it is classifying.
- When is std::decay_t the right choice rather than std::remove_cvref_t?When you are about to store or pass a copy and want the type a by-value parameter would have. Arrays then become pointers and function types become function pointers, which is what can actually be stored. `std::make_pair` and type-erased callables compute their stored types this way; classification code should use `remove_cvref_t`.
- How do you make std::conditional lazy when one branch type might not exist?Select between trait classes instead of their results, then read `::type` from the winner: `typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>, std::type_identity<T>>::type`. Naming `std::underlying_type<int>` is harmless; only asking for its missing `type` would fail, and the unselected trait is never asked.
remove_cvref is taking a parcel out of its envelope and peeling off the 'fragile' sticker before you ask what it is; decay is what the post office does when it re-packs it for shipping, which can turn a whole box of items into a slip that says where they are.
- is_enum_v sees through references, so Kind& counts as an enum.
- std::decay_t and std::remove_cvref_t always give the same result.
- std::conditional only forms the branch type it selects.
- remove_cvref_t turns a string literal's array type into const char*.
- std::remove_cvref has been in <type_traits> since C++11.
Assumes C++20 or later for std::remove_cvref_t and std::type_identity; C++17 code composes remove_cv_t and remove_reference_t instead.