amiga.org
     
iconAll times are GMT -6. The time now is 01:02 PM. | Welcome to Forum, please register to access all of our features.

» Amiga.org » Operating System Specific Discussions » Amiga OS » Amiga OS -- Development » More GNU C++ (ANSI C++ mode) oddness

Amiga OS -- Development This particular forum deals with issues regarding development for all versions of AmigaOS.

Reply
 
Thread Tools Display Modes
Old 03-26-2004, 08:25 AM   #1
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default More GNU C++ (ANSI C++ mode) oddness

Hi,

As I'm sure C++ people here know, according to the C++ standard, a member function of a class can be made an inspector (ie guarenteed not to change data belonging to the class) by qualifying with const.

As a synthetic example:

-edit-

Forgot to put "static" before getInstanceCount() when hurridly posting example

-/edit-

Code:
[size=x-small]
class Dummy {
  private:
    static int instances; // current number of Dummy objects
    int value;
    
  //..snip..
  public:
    // inspector methods
    static int getInstanceCount() const { return instances; }
    int getValue() const { return value; }
  //..snip..
    Dummy() { instances++; }
    ~Dummy() { instances--; }
};
[/size]
In my old code (compiled under that champion of ANSI C++ conformity StormC v3 ()), I qualified inspection methods as const and it all worked fine.

Now, I'm moving all my old code to GCC/C++ with ANSI C++ compiler model. It's been keeping me off the streets and out of trouble and all...

I just stumbled upon something quite odd. In the above example, the getInstanceCount() cannot be qualified const.

I just did some quick checks of the literature and basically it said that any normal member function (constructors and destructors aside) of a class can be const qualified to make it explicit that it doesn't have rights to change the representation.

However, an equally brisk google showed various developer forums where this problem cropped up all over using GCC.

Is there some fundamental reason for this that I'm missing or what?
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 09:00 AM   #2
TheJackal
Too much caffeine
Points: 4,395, Level: 42 Points: 4,395, Level: 42 Points: 4,395, Level: 42
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Posts: 95
Default Re: More GNU C++ (ANSI C++ mode) oddness

have you tried

Code:
[size=x-small]
class Dummy {
  private:
    static int instances; // current number of Dummy objects
    int value;
    
  //..snip..
  public:
    // inspector methods
    int getInstanceCount() const { return Dummy::instances; }
    int getValue() const { return value; }
  //..snip..
    Dummy() { Dummy::instances++; }
    ~Dummy() { Dummy::instances--; }
};
[/size]
Since the var is static and belongs to the class.
__________________
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\"Don\'t make me dance,.... You wouldn\'t like me when I dance.\" - The Hulk
TheJackal is offline   Reply With Quote
Old 03-26-2004, 09:07 AM   #3
PiR
Too much caffeine
Points: 4,885, Level: 44 Points: 4,885, Level: 44 Points: 4,885, Level: 44
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2003
Posts: 148
Default Re: More GNU C++ (ANSI C++ mode) oddness

Hi Karlos

A specialist in finding GNU bugs, huh? ;-)

I belive you're right and that should be accepted.
However I think I understand the reason why this behaves like so.

In C++ to C metacode conversion for methods you could imagine an additional argument:

void C::Method( ... ); -> void C_Metchod( C *this, ... );

And for inspectors (I was at important talk recently and someone was checking my knowledge and asked me about this and I didn't know the name...) this changes into:

coid C::Inspect( ... ) const; -> void C_Inspect( const C *this, ... );


The reason for this behaviour can be that your method is inlined and then compiler discovers that 'this' is not used, so it removes it completely. If it does not exist it cannot be const.

However, like you, I think this is not correct reaction.

My last conclusion is that your method is actually static function and its best declaration would be:

static int getInstanceCount() { return instances; }

as 'this' is really not needed here.

And I've met this already somewhere that static methods cannot be declared as inspectors - I belive its because of the above reason.

On the other hand this mayby should be accepted also?


Cheers
PiR is offline   Reply With Quote
Old 03-26-2004, 09:07 AM   #4
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

@The Jackal

-edit-

Actally, I need this particular method to be static (my fault, I missed the static kewyord in the edxample - fixed now). Simply defining it a normal member that returns the static value is no good, since the use since the function is meant to be called without needing an object, eg:

printf("Num instances of Dummy : %d\n", Dummy::getInstanceCount());

-/edit-

It seems it doesn't like the notion that a static member function can be const qualified when there is no obvious reason for this that I can see.

Naturally a static member function would only be able to access static data belonging to the class, but that's no reason why it should be forbidden from becoming a pure inspector of that static data.
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 09:09 AM   #5
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

@PiR

It does the same thing regardless of whether the function is defined inline or not.

I only made the Dummy example inline to save space in the post :-D
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 09:18 AM   #6
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

@All

Sorry, I missed the static qualifier for the getInstanceCount() method in the example I posted.

Of course, I meant

static int getInstanceCount() const { return instances; }

It is the above combination of static and const qalification that GNU is not happy with...
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 09:39 AM   #7
PiR
Too much caffeine
Points: 4,885, Level: 44 Points: 4,885, Level: 44 Points: 4,885, Level: 44
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2003
Posts: 148
Default Re: More GNU C++ (ANSI C++ mode) oddness

Ok Karlos

I understand and can share your doubts - inspector that does not change static members.

However I belive that the implementation is as I described - const or not const 'this' pointer. And static members are sort of globals, only their names are mixed with the name of the class... And how could you declare a function that does not modify global values?

The place that I met, rejecting static inspector is HP-UX aCC compiler, quite new one.

Cheers
PiR is offline   Reply With Quote
Old 03-26-2004, 10:01 AM   #8
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

Bleh,

It's discrepencies like this give C++ a bad name from java coders

I don't think this is neccesarily an ANSI issue (unless you know for sure it is) more of a quality of implementation issue with GNU.

Oh well, I can live without the static inspectors being explicitly const qualified. It's not like I use that many static member functions ;-)
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 10:17 AM   #9
TheJackal
Too much caffeine
Points: 4,395, Level: 42 Points: 4,395, Level: 42 Points: 4,395, Level: 42
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Posts: 95
Default Re: More GNU C++ (ANSI C++ mode) oddness

This also happens on msdev c++ compiler:

Code:
class Dummy 
{  
private:    
   static int instances;	// current number of Dummy objects    i
   int value;			    //..snip..  

public:    

   // inspector methods    
   static int getInstanceCount(void) const 
   { 
	return Dummy::instances; 
   }    
	
   int getValue() const { return value; } 

   //..snip..    

   Dummy() { instances++; }    
   ~Dummy() { instances--; }
};
Either remove the static before the getInstanceCount or the const after it will make it compile. I think the issue is to do with the const since a static function doesn't have a this pointer (as mentioned above?).

I've asked a guy here at work who is a c++ boffin, I'll let you know what his reply is.

[edit] his reply is this:
the static modifier on a class member function means that no data within the current object is modified by this function. As the static member function (and the static member variable you're accessing with it) doesn't belong to any object, the const modifier doesn't apply. Hence, you can't use it so just leave it off.
[/edit]
__________________
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\"Don\'t make me dance,.... You wouldn\'t like me when I dance.\" - The Hulk
TheJackal is offline   Reply With Quote
Old 03-26-2004, 10:38 AM   #10
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

Thanks.

I appreciate the argument, but can you ask your guy what his view on a class inspector method is? That is, a static function that explicitly inspects static data belonging to the class, as opposed to inspecting data belonging to an instance of the class.

I'm pretty curious as to the reasoning ;-)
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 10:45 AM   #11
TheJackal
Too much caffeine
Points: 4,395, Level: 42 Points: 4,395, Level: 42 Points: 4,395, Level: 42
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Posts: 95
Default Re: More GNU C++ (ANSI C++ mode) oddness

My answer would be that a static member function can only access the static member varables of that class, not of any instances of that class. (Unless of course you passed in a pointer to an instance of a class!)
__________________
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\"Don\'t make me dance,.... You wouldn\'t like me when I dance.\" - The Hulk
TheJackal is offline   Reply With Quote
Old 03-26-2004, 10:48 AM   #12
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

Quote:
TheJackal wrote:
My answer would be that a static member function can only access the static member varables of that class, not of any instances of that class.
Granted. But that is no reason to assume it should have any automatic right to always be allowed to modify any of those static members, is it?
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 10:54 AM   #13
TheJackal
Too much caffeine
Points: 4,395, Level: 42 Points: 4,395, Level: 42 Points: 4,395, Level: 42
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Posts: 95
Default Re: More GNU C++ (ANSI C++ mode) oddness

my friends reply to my reply is :

Quote:
Indeed you are correct, good sir.

Unless the static member function is passed a pointer to an instance of the class (or a pointer to an instance of the class is stored in a static member variable), if this is the case then the static member function can use the pointer to access members of that instance. It still can't be declared const though.
__________________
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\"Don\'t make me dance,.... You wouldn\'t like me when I dance.\" - The Hulk
TheJackal is offline   Reply With Quote
Old 03-26-2004, 10:57 AM   #14
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: More GNU C++ (ANSI C++ mode) oddness

Exactly.

A static member could always change an instance of the class that it is given access to, and can always change non constant data belonging to the class itself, regardless of wether that should be allowed or not.

I can't help feeling the lack of const declaration for static member inspectors is a total oversight

-edit-

Programmers :-D Man, do we know how to party on friday night, or what?

__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 03-26-2004, 11:02 AM   #15
TheJackal
Too much caffeine
Points: 4,395, Level: 42 Points: 4,395, Level: 42 Points: 4,395, Level: 42
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Posts: 95
Default Re: More GNU C++ (ANSI C++ mode) oddness

Quote:
-edit-

Programmers :-D Man, do we know how to party on friday night, or what?

Yeah, Even the com port party setting on my machine says it all, "-none-".



__________________
_________________
Any views, opinions, statements or advice in this message are solely those of the author and do not necessarily represent those of any organisation or individuals.

\"Don\'t make me dance,.... You wouldn\'t like me when I dance.\" - The Hulk
TheJackal is offline   Reply With Quote
Reply

Bookmarks

Tags
ansi , oddness , c++ , mode , gnu

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ANSI C++ forbids declaration `res' with no type EDanaII Amiga OS -- Development 18 05-03-2009 09:39 AM
ansi capable telnet application for OS3.x? AmiKit Amiga Software Issues and Discussion 5 04-09-2009 12:37 PM
How do I check size/date of a file in ANSI C? ChaosLord Amiga OS -- Development 10 02-16-2006 04:17 AM
Karlos' weekly GNU/ANSI C++ on amiga bash :-) Karlos Amiga OS -- Development 5 04-08-2004 04:43 PM
Longs, ints and printf (ansi-C) elendil Amiga OS -- Development 7 03-15-2004 08:56 AM