부스트/any.hpp (버전 1.55) (라인 263) 원래 객체 boost :: any_cast (const any)는 const_cast <>를 사용합니다 - 잠재적으로 UB가 아닌가요?
같이const
선언되지 않은 경우에 정의되지 않은 동작 될 수
const_cast<>
,
를 사용하지만 template<typename ValueType>
inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
{
return any_cast<ValueType>(const_cast<any *>(operand));
}
를 정의
class foo
{
boost::any value;
template<typename T>
foo(T const&x) noexcept : value(x) {}
template<typename T>
const T*ptr() const noexcept
{ return boost::any_cast(value); }
};
그럼 부자가 되겠습니까?
당신이 잘못 인용하지 않았습니까? UB 원래 const가 아닌 obj에서 const를 제거하면 어떻게됩니까? – sp2danny
함수가 const가 아닌 값 유형에 대한 포인터를 반환하면 UB *로 이어질 수 있지만 그렇지 않습니다. 상수, 상수. 함수 내부의 코드? 그것은 단지 캐스팅이고 객체를 수정하지 않습니다. –
흠. 나는 오래된 기준에 의해 잘못 인도되었을지도 모른다. 그래서'const_cast <> (non_const_object)'는 UB가 아닙니다. – Walter