请问有从T = std::shared_ptr<const A>
到TCV = A
的转换方式吗?我用这个:
template<typename T> struct is_shared_ptr : std::false_type {};
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
template<typename T>
concept is_shared = is_shared_ptr<T>::value;
template<typename T, typename U>
requires is_shared<T> and is_shared<U>
static void operate(const T& x, const U& y)
{
using TCV = std::remove_cv<typename decltype(T)::element_value>;
using UCV = std::remove_cv<typename decltype(U)::element_value>;
forward_operate(const_cast<TCV>(*x), const_cast<UCV>(*y));
};
forward_operate
的签名是:
template<typename A, typename B>
forward_operate(A&, B&);
这个代码不工作(ofc),你能帮忙吗?另外,我是否应该这样做(我需要这样做)?
发布于 2022-04-23 22:48:07
从std::shared_ptr到T的铸造
不能从(智能)指针向指定类型进行转换。您必须通过指针间接访问指定对象。
根据尝试的代码,看起来您正在尝试这样做:
forward_operate(
const_cast<typename T::element_type&>(*x),
const_cast<typename U::element_type&>(*y));
请记住,抛弃const是一种强烈的代码气味。除非你明白它的作用,否则不要这样做。
https://stackoverflow.com/questions/71984022
复制相似问题