constvolatile.cpp:91:9:错误:传递“易变性GENERIC_COMMAND”作为“最常见的参数”GENERIC_COMMAND& GENERIC_COMMAND :: operator =(const T&)[with T = COMPLETION_WAIT,GENERIC_COMMAND = GENERIC_COMMAND]â丢弃限定符[-fpermissive]
#include <iostream>
using namespace std;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
union GENERIC_COMMAND
{
struct
{
uint64_t first_operand : 60;
uint64_t opcode : 4;
uint64_t second_operand : 64;
};
struct
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
struct
{
uint64_t qword1;
uint64_t qword2;
};
GENERIC_COMMAND() {
}
GENERIC_COMMAND(volatile const GENERIC_COMMAND&){}
template <typename T>
volatile GENERIC_COMMAND& operator=(const T& rhs) volatile
{
this->dword1 = rhs.dword1;
this->dword2 = rhs.dword2;
this->dword3 = rhs.dword3;
this->dword4 = rhs.dword4;
return *this;
}
};
union COMPLETION_WAIT
{
struct
{
uint64_t s : 1;
uint64_t i : 1;
uint64_t f : 1;
uint64_t store_address : 49;
uint64_t reserved1 : 8;
uint64_t opcode : 4;
uint64_t store_data : 64;
};
struct
{
uint32_t dword1;
uint32_t dword2;
uint32_t dword3;
uint32_t dword4;
};
};
void add_completion_wait_command(uint32_t s, uint32_t i, uint32_t f,
uint64_t store_address, uint64_t store_data,
bool auto_flush)
{
COMPLETION_WAIT command;
command.dword1 = 0;
command.dword2 = 0;
command.dword3 = 0;
command.dword4 = 0;
command.s = s;
command.i = i;
command.f = f;
command.store_address = store_address >> 3;
command.opcode = 0x1;
command.store_data = store_data;
GENERIC_COMMAND generic;
static_cast<GENERIC_COMMAND>(generic = command);
}
main()
{
cout<< "in main"<< endl;
volatile GENERIC_COMMAND* A;//this has to be volatile only.
COMPLETION_WAIT cw;
A = new volatile union GENERIC_COMMAND;
static_cast<GENERIC_COMMAND>(A[0] = cw);
}
答案 0 :(得分:3)
您的运营商=需要不稳定。就像成员函数需要在const对象上调用const一样。如果你的成员函数不是volatile,编译器会优化它的主体,这不是你想要的volatile对象。所以语言有这个有用的规则。
众所周知,即使使用隐式声明的operator =,也会发生此错误,并将其记录为C语言的不兼容之一。
编辑:我想提一下你分配给的对象不是易失性的,所以即使用于访问它的指针具有volatile限定符,编译器也可以自由地对其进行优化操作。将volatile添加到它的类型new volatile union GENERIC_COMMAND