Templates and static variables and optimization
Templates and static variables and optimization
- Subject: Templates and static variables and optimization
- From: Lars Pastewka <email@hidden>
- Date: Wed, 12 Nov 2003 17:52:54 +0100
Hello!
I've encountered a really strange problem that only occurs when I use
optimization (-O2 or -O3). I've got the following piece of code.
template <class T>
class SmartEnum
{
... some code ...
protected:
SmartEnum(const string& name) {
fName = name;
fOrdinal = cardinality();
if(!mapF().insert(map<string, const T*, less<string>
>::value_type(name, (T*) this)).second) {
throw gError("SmartEnum::SmartEnum", "Unable to register
object " + name + " .");
}
vectorF().push_back((T*) this);
}
private:
static map<string, const T*, less<string> >& mapF() {
static map<string, const T*, less<string> > aMap;
return aMap;
}
static vector<const T*>& vectorF() {
static vector<const T*> aList;
return aList;
}
}
In my case, this enum class is used as an object factory in the
following way:
//---- Factories ----
class Postprocessor_Factory: public SmartEnum<Postprocessor_Factory>
{
public:
virtual Postprocessor *instantiate(Node *node) const = 0;
protected:
Postprocessor_Factory(const string &name)
: SmartEnum<Postprocessor_Factory>(name) { }
};
template <class T>
class Postprocessor_Register: public Postprocessor_Factory
{
public:
Postprocessor_Register(const string &name)
: Postprocessor_Factory(name) { }
virtual Postprocessor *instantiate(Node *node) const;
};
//---- Inline functions ----
template <class T>
inline Postprocessor *Postprocessor_Register<T>::instantiate(Node
*node) const
{
return new T(node);
}
Registration of a class is done by
const Postprocessor_Register<OutputFile> output_file("OutputFile");
What's supposed to happen is that Postprocessor_Factory gets it's own
version of the static variables aMap and aList. When using -O or -O1
this works fine, but when enabling a higher level of optimization,
suddenly the objects vectorF() and mapF() return is has not elements,
i.e. the methods size() return zero (and sometimes only one of them is
empty, which is even weirder). My guess is that "static vector<const
T*> aList;" creates a fresh vector when vectorF() is being called. Has
anybody encountered similar problems and does know a way to fix my
code? I'd be very grateful for any help,
Lars
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.