Topic: static constructors/destructors
Author: "Branimir Maksimovic" <bmaxa@volomp.com>
Date: 15 Oct 2005 05:00:01 GMT Raw View
I didn't found any proposal for providing static constructors,
and destructors. Basically idea is to elliminate need for
special prurpose classes that have dummy static instance in order
to call initialization code, and to provide more elegant way
for atexit.
There is no need to change anything in syntax just to
allow static specifier for constructor and destructor.
class Example{
static const int i = 2,j;
static vector<int> v1,v2;
static Example():j(1),v1(10)
{
/* class initialization code */
}
static ~Example()
{
/* class cleanup code */
}
};
int Example::j;
vector<int> Example::v1,Example::v2(5);
In this case implementation would initialize j with 1 and v1 with
10. Attempt to initialize i and v2 in initializer list would produce
compile error since initializers are already specified.
If implementation can't see either constructor definition or
*all* definitions for static member variables in *same* translation
unit, it should produce compile error.
If there are no definitions of static member variables in
translation unit, it simply ignores both static constructor and
destructor.
Call to static constructor body should be made when initialization
of static members is finished, but before main.
Calls to static destructors should be made after main, in opposite
order of constructor calls.
This shouldn't be difficult to implement and brings much cleaner code.
Best wishes , Bane.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Ben Hutchings <ben-public-nospam@decadentplace.org.uk>
Date: Sat, 15 Oct 2005 15:29:47 CST Raw View
Branimir Maksimovic <bmaxa@volomp.com> wrote:
> I didn't found any proposal for providing static constructors,
> and destructors. Basically idea is to elliminate need for
> special prurpose classes that have dummy static instance in order
> to call initialization code, and to provide more elegant way
> for atexit.
<snip>
A similar facility is included in Daveed Vandevoorde's module
proposal (N1778).
--
Ben Hutchings
Always try to do things in chronological order;
it's less confusing that way.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Branimir Maksimovic" <bmaxa@volomp.com>
Date: Sat, 15 Oct 2005 15:29:32 CST Raw View
Branimir Maksimovic wrote:
I have to add corrections to my post.
>
> class Example{
> static const int i = 2,j;
> static vector<int> v1,v2;
> static Example():j(1),v1(10)
> {
> /* class initialization code */
> }
> static ~Example()
> {
> /* class cleanup code */
> }
> };
>
> int Example::j;
> vector<int> Example::v1,Example::v2(5);
>
class Example{
static const int i = 2,j;
static vector<int> v1,v2;
static Example();
static ~Example();
};
static Example::Example():j(1),v1(10)
// static is needed to disambiguate constructors
{
/* class initialization code */
}
static Example::~Example()
{
/* class cleanup code */
}
const int Example::i,Example::j;
vector<int> Example::v1,Example::v2(5);
> In this case implementation would initialize j with 1 and v1 with
> 10. Attempt to initialize i and v2 in initializer list would produce
> compile error since initializers are already specified.
> If implementation can't see either constructor definition or
> *all* definitions for static member variables in *same* translation
> unit, it should produce compile error.
This should be :
If implementation sees constructor definition in translation unit
and class has static member variables , then it searches for
definitions of all member variables. If all definitions
can't be found it produce compile error.
Same is valid for destructor.
If constuctor/destructor declarations are present in class and
definition can't be found it should produce link error.
> If there are no definitions of static member variables in
> translation unit, it simply ignores both static constructor and
> destructor.
This is not necessary if definition is not allowed in class
and I see that this restriction is necessary if there are no
static member variables in class.
So if implementation sees constuctor definition in a class and
there are no static member variables present,it should produce
compile error.
> Call to static constructor body should be made when initialization
> of static members is finished, but before main.
> Calls to static destructors should be made after main, in opposite
> order of constructor calls.
> This shouldn't be difficult to implement and brings much cleaner code.
>
> Best wishes , Bane.
I've tried my best to represent idea.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]