top of page

Qualifier removing traits

Lets talk about 3 very important type traits:



šŸ” std::remove_cv: Introduced in C++11, this trait removes const and volatile from the type, preserving references and keeping the type in the same form (sans cv qualifiers), beware if your type has a reference in it, the type that will be returned will not be changed. šŸ› ļø



šŸŒ± std::decay: Also born in C++11, it decays the type to its simplest form by stripping away all qualifiers. However, it's worth noting that it can pose challenges for arrays and functions, as arrays get decayed to pointers and qualifires are left on the type. For most types, though, it's a quick path to the fundamental type without qualifires. šŸ”„



šŸ”§ Until C++20, addressing removal of references with cv required a longer route:


using two traits, std::remove_cv_t<std::remove_reference_t<B>>. Though effective, it appears a bit daunting and is less readable. In most scenarios, std::decay does the job efficiently, showcasing the importance of choosing the right tool for the right task. šŸ§°



šŸš€ std::remove_cvref: Enter C++20 with a sweet addition;


a shortcut for the previous two traits: std::remove_cv_t<std::remove_reference_t<B>>. This upgrade brings brevity and cleanliness to your code. Consider using this for generic functions involving arrays or functions. šŸš€


In this video i will demonstrate how each one of this trait works and how to use them correctly.





Recent Posts

See All

Virtual Constexpr Functions

In the latest episode of the C++Next Ā series, we explore the fascinating world of constexpr polymorphism . This episode is packed with...

bottom of page