folly::copy_cvref_t

copy_cvref_t

Synopsis

Declared in <folly/Traits.h>

template<
    typename Src,
    typename Dst>
using copy_cvref_t = /* implementation-defined */::apply<remove_cvref_t<Dst>>;

Description

A trait alias to replace the cvref category of Dst with that of Src.

CAUTION: This is not what is typically wanted in a forwarding or deducing-this context, or in most cases of casting one reference to another. In such cases, the most appropriate tool would be like_t, or std::forward_like in C++23.

Some of the problems with forwarding via copy_cvref are:

  • Removing const from a Dst that is backed by a value is quite problematic. The case of static_cast<copy_cvref_t<...>>(dst) would yield a compile error. The case of a C-style cast, const_cast, reinterpret_cast, or functional case may compile but may have undefined behavior by treating non-writable memory as writable.

  • The Dst value would typically have an address, and the volatile qualifier is a function of that address, so it would be incorrect to derive that from Src.

like_t and forward_like avoid these problems.