tags:

views:

1323

answers:

6

After some find and replace refactoring I ended up with this gem:

const class A
{
};

What does "const class" mean? It seems to compile ok.

+11  A: 

If you had this:

const class A
{
} a;

Then it would clearly mean that 'a' is const. Otherwise, I think that it is likely invalid c++.

Evan Teran
Good old forgotten features of C++! I do remember doing stuff like that in C long ago thou.
Robert Gould
+10  A: 

What does "const class" mean? It seems to compile ok.

Not for me it doesn't. I think your compiler's just being polite and ignoring it.

Edit: Yep, VC++ silently ignores the const, GCC complains.

I'm going to say it looks like an edge case that VC++ is getting wrong
1800 INFORMATION
Looks like; the 'const' doesn't affect anything that I can find.
+24  A: 

The const is meaningless in that example, and your compiler should give you an error, but if you use it to declare variables of that class between the closing } and the ;, then that defines those instances as const, e.g.:


const class A
{
public:
    int x, y;
}  anInstance = {3, 4};

// The above is equivalent to:
const A anInstance = {3, 4};
Adam Rosenfield
I can't find a justification to make this an error. Compilers may emit warnings at will, but only the standard determines what counts as an error.
MSalters
Because you're declaring something as const that isn't. This is something that is never what you want to do, and so should be an error. The standard should determine what is correct; defining every wrong thing would be too much work.
Bernard
I think that the term used in the standard is "illformed". The standard doesn't say what isn't allowed, only what is. This isn't valid C++ syntax, therefore isn't allowed.
KeithB
It's an error because the storage class specifiers are only applicable if you have a declarator and in this example there is no declarator.
Richard Corden
7.1.5.1/1: "There are two cv-qualifiers, const and volatile. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty."
Johannes Schaub - litb
+2  A: 

It's meaningless unless you declare an instance of the class afterward, such as this example:

const // It is a const object...
class nullptr_t 
{
  public:
    template<class T>
      operator T*() const // convertible to any type of null non-member pointer...
    { return 0; }

    template<class C, class T>
    operator T C::*() const   // or any type of null member pointer...
    { return 0; }

  private:
    void operator&() const;  // Can't take address of nullptr

} nullptr = {};

An interim nullptr implementation if you're waiting for C++0x.

Matt Joiner
A: 

It would be nice to add it to the language. It would mean that every method and variable is implicitly const. Effectively, every instance of that class is automatically const. Here's one possible use:

const class DataTransferObject {
public:
  ~DataTransferObject() {}
  int getX() { return x_; }  // const method implied
  int getY() { return y_; }  // const method implied

private:
  friend class Factory;
  DataTransferObject(int x, int y): x_(x), y_(y) {}
  int x_;  // const implied after constructor exits
  int y_;  // const implied after constructor exits
};
Jay
That would be possible, but current C++ already has `const` references to achieve the same goal.
Philipp
A: 

c++ help says it extends const to classes... so what u r talking is rubbish, compiler isn't ignoring the const modifier it does have some meaning, but what?

GeekCPP