CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Threaded View

  1. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error when using volatile object in overload assignment operator.

    Quote Originally Posted by ritulguru View Post
    thanks for mentioning that.
    I am using g++ compiler. I have tried running the same on DEV C++ 4.9.9.2 bloodshed.
    Turn on the ANSI switch and you will see the same errors.
    Code:
    -Wall -pedantic
    I believe those are the switches for g++.

    The problem is that by default, the g++ compiler will compile non-standard C++ code. If you turn on the ANSI compiler switch, you will see the same errors I'm seeing now. The code you posted is not ANSI C++, it is C++ with compiler extensions that are non-standard. Once you start using extensions, then all bets are off as to how your code will behave (then it's your responsibility to read the manual).
    I do not get any warning which you are getting.
    Turn on the ANSI switch and you will get the errors with the g++ compiler. The g++ compiler must be able to compile C++ code without any extensions (and if extensions are used, it must emit an error), else it can't claim to be an ANSI C++ compiler.
    Sorry, I am not familiar to Comeau C++ compiler.
    It is an online, ANSI C++ compiler. If the code fails to compile on that compiler, chances are great that the code is not compliant. Many professional C++ programmers who want to test if their code is compliant go to the on-line Comeau compiler to test their code. Obviously your code is not compliant.
    You can try this code on either g++(linux) or Dev C++(windows).
    No need to. I know if I set the right switches, the code will fail to compile. And BTW, the Dev C++ is not a compiler -- it uses g++ 3.x underneath the hood, so you see the same issues with both g++ under Linux and g++ using Dev-C++.

    Here is the code that must compile with all ANSI C++ compilers.
    Code:
    #include <iostream>
    using namespace std;
    
     typedef unsigned int uint32_t;
     typedef unsigned long long  uint64_t;
    
       union GENERIC_COMMAND
        {
            struct s1
            {
                uint64_t first_operand  :   60;
                uint64_t opcode         :   4;
                uint64_t second_operand :   64;
            };
    
            struct s2
            {
                uint32_t dword1;
                uint32_t dword2;
                uint32_t dword3;
                uint32_t dword4;
            };
    
            struct s3
            {
                uint64_t qword1;
                uint64_t qword2;
            };
    
            s1 m_s1;
            s2 m_s2;
            s3 m_s3;
    
            template <typename T>
            const GENERIC_COMMAND& operator=(const T& rhs)
            {
                m_s2.dword1 = rhs.m_s2.dword1;
                m_s2.dword2 = rhs.m_s2.dword2;
                m_s2.dword3 = rhs.m_s2.dword3;
                m_s2.dword4 = rhs.m_s2.dword4;
                return *this;
            }
        };
    
     
     union COMPLETION_WAIT
        {
            struct s1
            {
                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 s2
            {
                uint32_t dword1;
                uint32_t dword2;
                uint32_t dword3;
                uint32_t dword4;
            };
            s1 m_s1;
            s2 m_s2;
        };
    
     
     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.m_s2.dword1 = 0;     
            command.m_s2.dword2 = 0;    
            command.m_s2.dword3 = 0;    
            command.m_s2.dword4 = 0;     
    
            command.m_s1.s = s;
            command.m_s1.i = i;
            command.m_s1.f = f;
            command.m_s1.store_address = store_address >> 3;
            command.m_s1.opcode = 0x1;
            command.m_s1.store_data = store_data;
    
            GENERIC_COMMAND generic;
            generic = command;
           
        }
    
    int main()
    {
        cout<< "in main"<< endl;
        GENERIC_COMMAND* A;
        COMPLETION_WAIT cw;
        A = new union GENERIC_COMMAND;
        A[0] = cw;
        //....
    }
    As you can see, there is no need for volatile. Maybe you believed that you needed volatile since you used all of that non-standard syntax.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 27th, 2012 at 10:37 AM. Reason: Corrected the g++ compiler switches

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured