5

I'm new to gcc, and I'm encountering a really strange problem while compiling precompiled header in gcc-4.7.2/4.7.3.

The codes below:

FooClass.h

#include <cstddef>
#include <X11/X.h>
#include <xmmintrin.h>
#include <emmintrin.h>
#include <smmintrin.h>

#ifndef FOO_CLASS_ERROR
#define FOO_CLASS_ERROR

class FooClass
{
public:
    union
    {
        struct
        {
            float x,y,z,w;
        };
        __m128 v;
    };

    FooClass( void )
    {
    }

    FooClass( const __m128 _v )
    : v( _v )
    {
    }

    FooClass( const FooClass& rhs )
    : v( rhs.v )
    {
    }

    FooClass operator -( void ) const;

} __attribute__( (aligned(16)) );

#endif

FooClass.cpp

#include "FooClass.h"

FooClass FooClass::operator -( void ) const
{
    return FooClass();
}

compiled as pch:

g++ -Wall -fexceptions -g -msse4.1 -Winvalid-pch -include "FooClass.h"  -c FooClass.h -o FooClass.h.gch
g++ -Wall -fexceptions -g -msse4.1 -Winvalid-pch -include "FooClass.h"  -c FooClass.cpp -o obj/Debug/FooClass.o

will generate errors:

./pch.h:40:17: error: prototype for ‘FooClass FooClass::operator-() const’ does not match any in class ‘FooClass’
./pch.h:36:14: error: candidate is: FooClass FooClass::operator-() const

I've checked for a whole afternoon, and found that:

Removing "const" or the copy constructor will solved these errors.

But I don't know why...Can somebody tell me the reason that causes this error? or maybe it's a gcc bug?

Lendy Zhang
  • 135
  • 7
  • 2
    Make sure you recreated the precompiled header after you changed `FooClass.h`. Sounds like the diagnostic has a slight bug due to a desync between the real header and the PCH. i.e. _do a fresh build_ and let us know the results. – Lightness Races in Orbit Aug 06 '13 at 11:57
  • 2
    This isn't the problem, but names that contain two consecutive underscores (`__FooClass__`) and names that begin with an underscore followed by a capital letter are reserved to the implementation. Don't use them. – Pete Becker Aug 06 '13 at 11:59
  • Yes, I did make a clean build. And i'm sorry for the bad code style, but I'm just trying to make a minimum piece of codes that can reproduce this problem, so please ignore the code style :-) – Lendy Zhang Aug 06 '13 at 12:07
  • While not related to your error, you should be very, very careful with the "idiom" you're employing, i.e. the union containing an `__m128`. If you need to access the components of the `__m128`, you should do explicit loads and stores, since doing it this way may not guarentee correctness. For more information, http://stackoverflow.com/questions/9241721/is-it-possible-to-access-internal-values-in-a-m128-variable-as-attribute-in-a –  Aug 06 '13 at 12:32
  • I just tried your example verbatim locally with no errors in G++ 4.4.4 and 4.8.0. It fails, however, with G++ 4.6.0. I suspect it _is_ actually a G++ issue. – Joe Z Aug 06 '13 at 13:18
  • Thank you very much. I will upgrade my gcc to 4.8.0 and try again. – Lendy Zhang Aug 06 '13 at 13:28
  • Tried to compile this with clang (Apple LLVM 4.2). Compiles ok. – Alexander B Nov 13 '13 at 20:18

1 Answers1

0

That's a very strange error. Test what happens if you inline the operator:

FooClass operator -( void ) const
{
    return FooClass();
}