// { dg-do run }
|
// { dg-do run }
|
// This tests two things:
|
// This tests two things:
|
// 1. there is an annoying warning.
|
// 1. there is an annoying warning.
|
// singleton.C:26: warning: `class singleton' only defines private constructors and has no friends
|
// singleton.C:26: warning: `class singleton' only defines private constructors and has no friends
|
// egcs fails to see that there is a public static accessor function.
|
// egcs fails to see that there is a public static accessor function.
|
// 2. the program crashes, because apparently the static variable s in
|
// 2. the program crashes, because apparently the static variable s in
|
// singleton::instance() is considered constructed although the ctor
|
// singleton::instance() is considered constructed although the ctor
|
// exited via an exception. (crash changed to nonzero return here)
|
// exited via an exception. (crash changed to nonzero return here)
|
|
|
class singleton {
|
class singleton {
|
public:
|
public:
|
static singleton& instance() {
|
static singleton& instance() {
|
static singleton s;
|
static singleton s;
|
return s;
|
return s;
|
}
|
}
|
int check() {return initialized;}
|
int check() {return initialized;}
|
|
|
private:
|
private:
|
singleton() : initialized(1) {
|
singleton() : initialized(1) {
|
if ( counter++ == 0 ) throw "just for the heck of it";
|
if ( counter++ == 0 ) throw "just for the heck of it";
|
initialized = 2;
|
initialized = 2;
|
}
|
}
|
singleton( const singleton& rhs );
|
singleton( const singleton& rhs );
|
void operator=( const singleton& rhs );
|
void operator=( const singleton& rhs );
|
int initialized;
|
int initialized;
|
static int counter;
|
static int counter;
|
};
|
};
|
|
|
int singleton::counter;
|
int singleton::counter;
|
|
|
int main()
|
int main()
|
{
|
{
|
while (1) {
|
while (1) {
|
try {
|
try {
|
return singleton::instance().check()-2;
|
return singleton::instance().check()-2;
|
} catch (...) { }
|
} catch (...) { }
|
}
|
}
|
}
|
}
|
|
|
|
|