F
Frederick Gotham
Albo posted:
The C++ Standard does such a thing already -- it has measures in place
which simply scorn "stupid" use of classes. Below is an example of code
which SHOULD work properly, but which the C++ Standard says need not work
properly, because it deems it to be "stupid" use of a class:
class ArbitraryClass {
public:
static unsigned counter;
ArbitraryClass()
{
++counter;
}
ArbitraryClass( const ArbitraryClass & )
{
++counter;
}
};
unsigned ArbitraryClass::counter = 0;
class Manipulator {
public:
~Manipulator()
{
if ( 2 == ArbitraryClass::counter ) return;
/* Now some gratuitous undefined behaviour: */
int *p;
*p = 42;
}
} global_object;
int main()
{
ArbitraryClass obj = ArbitraryClass();
}
The C++ Standard gives explicit permission for only one object of
ArbitraryClass to be constructed in the above code, and by implication,
decrees that it's "stupid" for someone to depend on an extra instance of
their class being created. As you put it, Albo, this certainly puts
limitations on what an object can make internally for his own purposes.
It is unacceptable that you put arbitrary limitations on what an
object can make internally for his own purposes, just to make your
container easier to program. Nobody will use such a container.
The C++ Standard does such a thing already -- it has measures in place
which simply scorn "stupid" use of classes. Below is an example of code
which SHOULD work properly, but which the C++ Standard says need not work
properly, because it deems it to be "stupid" use of a class:
class ArbitraryClass {
public:
static unsigned counter;
ArbitraryClass()
{
++counter;
}
ArbitraryClass( const ArbitraryClass & )
{
++counter;
}
};
unsigned ArbitraryClass::counter = 0;
class Manipulator {
public:
~Manipulator()
{
if ( 2 == ArbitraryClass::counter ) return;
/* Now some gratuitous undefined behaviour: */
int *p;
*p = 42;
}
} global_object;
int main()
{
ArbitraryClass obj = ArbitraryClass();
}
The C++ Standard gives explicit permission for only one object of
ArbitraryClass to be constructed in the above code, and by implication,
decrees that it's "stupid" for someone to depend on an extra instance of
their class being created. As you put it, Albo, this certainly puts
limitations on what an object can make internally for his own purposes.