Monday, February 2, 2015

Digest for comp.lang.c++@googlegroups.com - 25 updates in 3 topics

Christian Gollwitzer <auriocus@gmx.de>: Jan 26 10:32PM +0100

Am 26.01.15 um 22:30 schrieb Christian Gollwitzer:
 
> Even if you use Borland C++ for production work, it is ALWAYS a good
> idea to install another compiler to track down such errors. Here is what
> I get from clang:
 
And here comes gcc:
 
Apfelkiste:Tests chris$ g++-mp-4.6 nocomp.cpp
nocomp.cpp: In member function 'bool Tester<T>::CompareObject(const T&)
[with T = Foo]':
nocomp.cpp:55:41: instantiated from here
nocomp.cpp:40:20: error: no match for 'operator==' in
'((Tester<Foo>*)this)->Tester<Foo>::object == obj'
Apfelkiste:Tests chris$
 
Both error messages are way more helpful than the illegal something that
your compiler reports.
 
 
Christian
Rosario193 <Rosario@invalid.invalid>: Jan 27 09:23AM +0100

On Mon, 26 Jan 2015 16:00:28 -0500, DSF wrote:
...
 
>DSF
>"'Later' is the beginning of what's not to be."
>D.S. Fiscus
 
this compile and run here...
#include <stdio.h>
 
class Foo
{public:
Foo(int n): num(n){;}
Foo(Foo& a){if(&a!=this) num=a.num;}
friend int operator==(Foo& f1, Foo& f2);

int num;
};
 
int operator==(Foo& f1, Foo& f2){return f1.num==f2.num;}
 
template<class T>class Tester
{public:
int CompareObject(T& obj){return object==obj;}
Tester(T& obj):object(obj){;}
 
T object;
};
 
 
int main(void)
{Foo fo1(12);
Foo fo2(12);
Tester<Foo> testfoo(fo1);
int result=testfoo.CompareObject(fo2);

printf("fo1 %s equal fo2\n", result ? "does" : "does not");
return 0;
}
Bo Persson <bop@gmb.dk>: Jan 27 12:17PM +0100

On 2015-01-27 09:22, DSF wrote:
 
 
> Note the missing ampersand on the second Foo of the inline line.
> Shouldn't this be an error? The TU compiled with 0 errors and 0
> warnings.
 
That's a different operator that doesn't match the friend declaration.
It will compare foo1 against a copy of foo2, but is perfectly legal.
 
 
> BTW, adding the ampersand did not fix the main problem.
 
Too bad.
 
 
Bo Persson
"Tobias Müller" <troplin@bluewin.ch>: Jan 26 09:57PM

>> etc
 
> The entire message, as stated before, is: "Illegal structure
> operation". See my answer to Ian Collins for more.
 
Well to be precise what I wanted to say was:
"All compilers that I know give you some meaningful information. What
compiler could you possibly be using that doesn't give you that?"
 
>> What compiler are you using?
 
> Borland C/C++ 5.01A
 
> Yes, I know.
 
Do you realize that this is almost 20 years old?
 
Tobi
Rosario193 <Rosario@invalid.invalid>: Jan 27 10:05AM +0100

On Tue, 27 Jan 2015 09:23:33 +0100, Rosario193 wrote:
 
 
> printf("fo1 %s equal fo2\n", result ? "does" : "does not");
> return 0;
>}
 
_TEXT segment dword public use32 'CODE'
@$beql$qr3Foot1 segment virtual
@@$beql$qr3Foot1 proc near
?live16385@0:
;
; int operator==(Foo& f1, Foo& f2){return f1.num==f2.num;}
;
push ebp
mov ebp,esp
@1:
mov eax,dword ptr [ebp+8]
mov edx,dword ptr [eax]
mov ecx,dword ptr [ebp+12]
cmp edx,dword ptr [ecx]
sete al
and eax,1
@3:
@2:
pop ebp
ret
@@$beql$qr3Foot1 endp
@$beql$qr3Foot1 ends
_TEXT ends
_TEXT segment dword public use32 'CODE'
_main segment virtual
@_main proc near
?live16386@0:
;
; int main(void)
;
push ebp
mov ebp,esp
add esp,-12
;
; {Foo fo1(12);
;
@4:
mov dword ptr [ebp-4],12
;
; Foo fo2(12);
;
mov dword ptr [ebp-8],12
;
; Tester<Foo> testfoo(fo1);
;
lea eax,dword ptr [ebp-4]
lea edx,dword ptr [ebp-12]
cmp eax,edx
je short @5
mov ecx,dword ptr [ebp-4]
mov dword ptr [ebp-12],ecx
 
here appear that is called
"Foo(Foo& a){if(&a!=this) num=a.num;}"
inline
 
;
; int result=testfoo.CompareObject(fo2);
;
@5:
@6:
lea eax,dword ptr [ebp-8]
push eax
lea edx,dword ptr [ebp-12]
push edx
call @@$beql$qr3Foot1
add esp,8
 
here it call the right function
int operator==(Foo& f1, Foo& f2){return f1.num==f2.num;}
 
and printf result:
 
;
;
; printf("fo1 %s equal fo2\n", result ? "does" : "does not");
;
?live16386@80: ; EAX = result
test eax,eax
je short @7
mov ecx,offset s@+18
jmp short @8
@7:
mov ecx,offset s@+23
@8:
push ecx
push offset s@
call @_printf
add esp,8
;
; return 0;
;
?live16386@96: ;
xor eax,eax
;
; }
;
@10:
@9:
mov esp,ebp
pop ebp
ret
@_main endp
_main ends
DSF <notavalid@address.here>: Jan 26 04:00PM -0500

On Mon, 26 Jan 2015 08:33:06 +1300, Ian Collins <ian-news@hotmail.com>
wrote:
 
>> Hello, group!
 
>> I have a template class that requires "==" to be overloaded in any
>> class that uses it. If the class doesn't overload "==", I get the
**>> compile time message "Illegal structure operation" on the "=="
>> template class or has "==" overloaded.
 
>You need to provide more detail - example code that fails to compile and
>the exact error message.
 
** No offense, but I gave you the exact error message.
 
Below is a mock-up of the problem.
 
For the pedants out there, yes I know include files aren't supposed
to have an extension. Dumbest idea! If you don't think so, try
searching an entire drive for all header files. Also, I prefer printf
over <<, so shoot me! :o)
 
As is, the code below will produce the "Illegal structure operation"
on the line:
return object == obj;
 
Remark out #define NOCOMPILE and it will compile and run.
 
This is because the first declaration for object Foo does not
overload operator ==, while the second does.
 
In my original source code, the TU that generates this error does
not even use the template represented by "Tester". All other TUs
compile error-free.
 
#include <stdio.h>
 
#define NOCOMPILE
 
#ifdef NOCOMPILE
class Foo
{
public:
Foo(int n) : num(n){};
 
private:
int num;
};
 
#else
 
class Foo
{
public:
Foo(int n) : num(n){};
friend bool operator==(const Foo& f1, const Foo& f2);
 
private:
int num;
};
 
inline bool operator==(const Foo& f1, const Foo& f2)
{
return f1.num == f2.num;
}

No comments: