0

constvolatile.cpp:91:9:エラー:âvolatileGENERIC_COMMANDâをâconstGENERIC_COMMAND&GENERIC_COMMAND :: operator =(const T&)[with T = COMPLETION_WAIT、GENERIC_COMMAND=GENERIC_COMMAND]âの「this」引数として渡す-修飾子を破棄します

            #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);

            }
4

1 に答える 1

3

operator= は揮発性である必要があります。const オブジェクトで呼び出されるメンバー関数が const である必要があるのと同じように。メンバー関数が揮発性でない場合、コンパイラはその本体を最適化しますが、これは揮発性オブジェクトに必要なものではありません。したがって、言語にはこの便利なルールがあります。

このエラーは、暗黙的に宣言された operator= でも発生することがよく知られており、C 言語との非互換性の 1 つとして文書化されています。

編集:割り当て先のオブジェクトは揮発性ではないため、アクセスに使用されるポインターに揮発性修飾子が含まれていても、コンパイラーは自由に操作を最適化できます。その型に volatile を追加します

new volatile union GENERIC_COMMAND
于 2012-07-24T12:07:51.833 に答える