| 1 |
// { dg-do run } |
| 2 |
// This tests two things: |
| 3 |
// 1. there is an annoying warning. |
| 4 |
// singleton.C:26: warning: `class singleton' only defines private constructors and has no friends |
| 5 |
// egcs fails to see that there is a public static accessor function. |
| 6 |
// 2. the program crashes, because apparently the static variable s in |
| 7 |
// singleton::instance() is considered constructed although the ctor |
| 8 |
// exited via an exception. (crash changed to nonzero return here) |
| 9 |
|
| 10 |
class singleton { |
| 11 |
public: |
| 12 |
static singleton& instance() { |
| 13 |
static singleton s; |
| 14 |
return s; |
| 15 |
} |
| 16 |
int check() {return initialized;} |
| 17 |
|
| 18 |
private: |
| 19 |
singleton() : initialized(1) { |
| 20 |
if ( counter++ == 0 ) throw "just for the heck of it"; |
| 21 |
initialized = 2; |
| 22 |
} |
| 23 |
singleton( const singleton& rhs ); |
| 24 |
void operator=( const singleton& rhs ); |
| 25 |
int initialized; |
| 26 |
static int counter; |
| 27 |
}; |
| 28 |
|
| 29 |
int singleton::counter; |
| 30 |
|
| 31 |
int main() |
| 32 |
{ |
| 33 |
while (1) { |
| 34 |
try { |
| 35 |
return singleton::instance().check()-2; |
| 36 |
} catch (...) { } |
| 37 |
} |
| 38 |
} |
| 39 |
|