Welcome, Guest. Please login or register.

Author Topic: More GNU C++ (ANSI C++ mode) oddness  (Read 2916 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
More GNU C++ (ANSI C++ mode) oddness
« on: March 26, 2004, 03:25:33 PM »
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 :lol:

-/edit-

Code: [Select]
[size=x-small]
class Dummy {
  private:
    [b]static[/b] int instances; // current number of Dummy objects
    int value;
   
  //..snip..
  public:
    // inspector methods
    [b]static[/b] int getInstanceCount() [b]const[/b] { return instances; }
    int getValue() [b]const[/b] { return value; }
  //..snip..
    Dummy() { instances++; }
    ~Dummy() { instances--; }
};
[/size]


In my old code (compiled under that champion of ANSI C++ conformity StormC v3 (:lol:)), 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?
int p; // A
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show only replies by TheJackal
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #1 on: March 26, 2004, 04:00:33 PM »
have you tried

Code: [Select]
[size=x-small]
class Dummy {
  private:
    static int instances; // current number of Dummy objects
    int value;
   
  //..snip..
  public:
    // inspector methods
    int getInstanceCount() const { return [color=FF0000]Dummy::[/color]instances; }
    int getValue() const { return value; }
  //..snip..
    Dummy() { [color=FF0000]Dummy::[/color]instances++; }
    ~Dummy() { [color=FF0000]Dummy::[/color]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
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show only replies by PiR
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #2 on: March 26, 2004, 04:07:24 PM »
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
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #3 on: March 26, 2004, 04:07:42 PM »
@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.
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #4 on: March 26, 2004, 04:09:23 PM »
@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
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #5 on: March 26, 2004, 04:18:26 PM »
@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...
int p; // A
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show only replies by PiR
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #6 on: March 26, 2004, 04:39:34 PM »
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
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #7 on: March 26, 2004, 05:01:32 PM »
Bleh,

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

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 ;-)
int p; // A
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show only replies by TheJackal
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #8 on: March 26, 2004, 05:17:33 PM »
This also happens on msdev c++ compiler:

Code: [Select]

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
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #9 on: March 26, 2004, 05:38:23 PM »
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 ;-)
int p; // A
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show only replies by TheJackal
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #10 on: March 26, 2004, 05:45:40 PM »
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
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #11 on: March 26, 2004, 05:48:43 PM »
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?
int p; // A
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show only replies by TheJackal
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #12 on: March 26, 2004, 05:54:18 PM »
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
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #13 on: March 26, 2004, 05:57:02 PM »
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 :lol:

-edit-

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

:roflmao:
int p; // A
 

Offline TheJackal

  • Jr. Member
  • **
  • Join Date: Oct 2003
  • Posts: 95
    • Show only replies by TheJackal
Re: More GNU C++ (ANSI C++ mode) oddness
« Reply #14 on: March 26, 2004, 06:02:50 PM »
Quote

-edit-

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

:roflmao:


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

 :lol:

_________________
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