Posted by
Kakhkhor Abdijalilov on
URL: http://quantlib.414.s1.nabble.com/fix-for-intel-s-compiler-tp9222.html
Singleton pattern doesn't work with Intel's C++ compiler 11.1 on
Windows. I tested it both in 32 and 64 bit modes.
When optimization is enabled (I used O3), each translation unit ends
up creating its own instance of Singleton. For example, this code
fragment doesn't do what it is supposed to do.
// file1.cpp
Settings::instance().evaluationDate() = date1;
// file2.cpp
Settings::instance().evaluationDate() = date2; // file1.cpp still sees date1
// fie3.cpp
Date date3 = Settings::instance().evaluationDate(); // not equal to date2
Overwriting evaluation date in file2.cpp doesn't actually overwrite
it. The above code will create 3 instances of Settings class and the
program will eventually crash. Somehow static non-const local
variables and templates don't mix on ICC. I modified the original
implementation to uses const static variable and everything worked
well. The new implementation is attached.
Regards,
Kakhkhor Abdijalilov.
//-----------------------------------------
template <class T>
class Singleton : private boost::noncopyable {
public:
static T& instance() {
#if defined(QL_ENABLE_SESSIONS)
Integer id = sessionId();
#else
Integer id = 0;
#endif
boost::shared_ptr<T>& p = (*instances_)[id];
if (!p)
p.reset(new T);
return *p;
}
protected:
Singleton() {}
private:
typedef std::map<Integer, boost::shared_ptr<T> > map_type;
static const boost::scoped_ptr<map_type> instances_;
};
template <typename T>
const boost::scoped_ptr<Singleton<T>::map_type>
Singleton<T>::instances_(new Singleton<T>::map_type);
//-----------------------------------------
------------------------------------------------------------------------------
This SF.net email is sponsored by
Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev