在我的代码中,我有以下类(对于这个问题,显然有些简化):
template<typename T>
class MappedMemory
{
private:
T* localAddress;
public:
void* getGenericDataPointer()
{
return reinterpret_cast<void*>(localAddress);
}
}
当
void* getGenericDataPointer()
{
auto normalAddress = const_cast<typename std::remove_cv<T*>::type>(localAddress); //To cast away volatile and const
return reinterpret_cast<void*>(normalAddress);
}
null
error: reinterpret_cast from type ‘volatile unsigned int*’ to type ‘void*’ casts away qualifiers
return reinterpret_cast<void*>(normalAddress);
您需要将本身,而不是指向它的指针。
auto normalAddress = const_cast<typename std::remove_cv<T>::type*>(localAddress);
注意,