Skip navigation

i’m looking at the ideas in the draft standard for C++0x.

I don’t like some of the things they’re doing.

For example, this constexpr thing.

constexpr int GetFive() {return 5;}
 
int some_value[GetFive() + 5]; //create an array of 10 integers. legal C++0x

Does this not look like a bad idea to anyone?

To be completely honest, I’ve always hated the idea of writing getters and setters that do nothing else other than straight get and set.

For example:

class Number
{
private:
int val ;
public:
void setVal( int to ) {
this->val = to ;
}
int getVal() {
return this->val ;
}

};

I mean, come on. this is a trivial class, but the getter and setter are entirely superfluous in my opinion because they don’t __DO__ anything.

A getter and setter like this, on the other hand:

class Number
{
private:
int val ;
public:
void setVal( int to ) {
if( to >= 0 )
this->val = to ;
else
this->val = 0 ;
}
int getVal() {
return this->val ;
}
};

That makes SOME sense. In this second scenario, we’re protecting val from being assigned a negative value, which is forbidden for whatever reason.

THAT use of a getter and setter I approve of. But the transparent, simple, silly getter and setter functions like the red example above (that can be autogenerated by the IDE when you work in Java Netbeans or eclipse)

  1. DO NOTHING
  2. TAKE UP SPACE
  3. look worse in code, if not for anything but the extra 3 characters you have to write getW” and setW” instead of just a simple assignment w =
  4. Can be slower (function call overhead) if no effort is taken to inline

So that same idea applies to the constexpr thing. You don’t NEED IT.

Just write the constant 5. Or #define it.

I just don’t see the point of it.

constexpr adds additional complexity to the C++ programming language while not really providing new functionality at all. Its just repeating the same general pattern of transparent getters and setters that people seem to like to use.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s