Re: Initialize static members outside the class
On 16 Lug, 13:42, xtrigger...@gmail.com wrote:
On 16 Lug, 13:02, Michael DOUBEZ <michael.dou...@free.fr> wrote:
James Kanze a =E9crit :
On Jul 15, 5:04 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
Steven Woody a =E9crit :
[...]
There is still boost.assign:http://www.boost.org/doc/libs/1_35_0/lib=
s/assign/doc/index.html
If I understand correctly (I have never used it), this will give
something like:
const std::vector<int> magic_code=list_of(1)(9)(103)(2)...(42);
If vector<>::push_back supported chaining, you wouldn't even
need that:
The strcpy(), strcat, str... were designed for chaining but AFAIK, it i=
s
rarely used that way. I guess it would also be marginally useful for
std::vector<>.
std::vector< int > const magic
= std::vector< int >().push_back( 1 )
.push_bac=
k( 9 )... ;
That's hideous. :)
--
Michael
Hi to all,
I use the following template to trigger static member functions (that
can be used to do any type of initalization ).
It always seemed to work in the few cases I've used it. The only doubt
is the constructor of the template, that refers to the static member
object to enforce instantiation. My doubt is that some compiler
optimization might throw that away. With gcc it never seems to happen
with any optimization.
Any comment?
Regards to all,
Francesco
#include <iostream>
#include <vector>
template< typename T >
class CAutoInitializer
{
protected:
// enforce sInit instantiation
CAutoInitializer() { return; sInit; }
private:
struct CInit { CInit() { T::StaticInitializer(); } };
static CInit sInit;
};
template< typename T >
typename CAutoInitializer< T >::CInit CAutoInitializer< T >::sInit;
//
class CSomething : CAutoInitializer< CSomething >
{
public:
// avoid static init fiasco
static std::vector< int > & GetVec()
{ static std::vector< int > sVec; return sVec; }
static void StaticInitializer()
{
std::cout << "Do anything you want here\n";
GetVec().push_back( 10 );
}
};
//
class CSomethingElse : CAutoInitializer< CSomethingElse >
{
public:
static void StaticInitializer()
{ std::cout << "Do something else here\n"; }
};
int main()
{
std::cout << "main\n";
CSomething obj1;
CSomethingElse obj2;
std::cin.get();
}- Nascondi testo citato
- Mostra testo citato- Nascondi testo citato
- Mostra testo citato
Sorry,
the code I posted is broken if any other statically initialized object
will call GetVec() before the template static object is instantiated.
Fiasco!
The below code is more like it I think, but it's getting
convoluted.. :-(
Moreover it does not address multithreading issues.
Sorry again,
bye,
Francesco
#include <iostream>
#include <vector>
template< typename TDerivingClass, typename TStaticObject >
class CStaticInstanceAutoInit
{
public:
static TStaticObject & GetInstance()
{
static bool sInit = TDerivingClass::StaticInitializer();
return GetUnsafe();
}
protected:
static TStaticObject & GetUnsafe()
{
static TStaticObject sObj;
return sObj;
}
CStaticInstanceAutoInit() { return; sInit; }
private:
struct CInit { CInit()
{ CStaticInstanceAutoInit::GetInstance(); } };
static CInit sInit;
};
//
template< typename TDerivingClass, typename TStaticObject >
typename CStaticInstanceAutoInit< TDerivingClass, TStaticObject
::CInit
CStaticInstanceAutoInit< TDerivingClass, TStaticObject
::sInit;
//
class CSomething : public CStaticInstanceAutoInit< CSomething,
std::vector< int > >
{
public:
static bool StaticInitializer( void )
{
std::cout << "init static instance something\n";
GetUnsafe().push_back( 13 );
return true;
}
};
//
int main()
{
std::cout << "main\n";
CSomething obj1;
obj1.GetInstance();
obj1.GetInstance();
std::cin.get();
}