简体   繁体   中英

Removing const qualifier from type-inference return type

Quick question for the folks out there. I have the function signature below that under some but not all circumstances returns a const type qualifier on the return type.

template <typename U,
          typename V,
          std::enable_if_t<std::is_arithmetic<V>::value, int>/* = 0*/ > // SFINAE dummy
Vector3D<decltype(std::declval<U>() / std::declval<V>())> operator/ (const Vector3D<U>& p_vector, const Matrix3D<V>& p_matrix)

How can I use const_cast to remove any const qualifiers returned by decltype(std::declval<U>() / std::declval<V>()) ?

The answer is std::remove_const :

template <typename U,
          typename V,
          std::enable_if_t<std::is_arithmetic<V>::value, int>/* = 0*/ > // SFINAE dummy
Vector3D<std::remove_const_t<decltype(std::declval<U>() / std::declval<V>())>>
operator/ (const Vector3D<U>& p_vector, const Matrix3D<V>& p_matrix)

If U/V might return a reference you might want std::remove_cvref instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM