Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I kept get compiling error messages with following code:

getMap()->insert(std::make_pair(s, &createT<t>));</t>


1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\type_traits(197): error C2752: 'std::tr1::_Remove_reference<_Ty>' : more than one partial specialization matches the template argument list

How can fix this?

Thanks,




C++
class CGeometry;
template<typename T> CGeometry * createT() { return new T; };
struct BaseFactory
{
    typedef std::map<std::string, CGeometry*(*)()> map_type;
    static void RemoveAll()
    {
        if( map ) map->clear();
    }
    static CGeometry * createInstance(std::string const& s)
    {
        map_type::iterator it = getMap()->find(s);
        if(it == getMap()->end())
            return 0;
        return it->second();
    }
protected:
    static map_type * getMap()
    {
        // never delete'ed. (exist until program termination)
        // because we can't guarantee correct destruction order
        if(!map) { map = new map_type; }
        return map;
    }
private:
    static map_type * map;
};
template<typename T>
struct RegisterUserClass : BaseFactory
{
    RegisterUserClass( std::string const& s )
    {
        getMap()->insert(std::make_pair(s, &createT<T>));
    };
};
Posted
Updated 25-Feb-11 13:26pm
v2

Hi,

I suspect you forgot to #include <string> as your code compiles with VC2010.

BTW C++ can do lot of things at compile time but you seem to prefer having them done at runtime :sigh:
For instance:
C++
struct BaseFactory
{
    typedef std::map<std::string, CGeometry*(*)()> map_type;
    static void RemoveAll()
    {
        map.clear();
    }
    static CGeometry * createInstance(std::string const& s)
    {
        map_type::iterator it = map.find(s);
        return it == map.end() ? 0 : it->second();
    }
protected:
    static map_type map;
};
is equivalent to your construct, simpler and more efficient.

cheers,
AR
 
Share this answer
 
v2
Sorry, the code can be compiled by itself.
But when other class use this class, it will generates compile error.
It works just fine with VS 2008. The error only happens with VS 2010.
The error only happens with following code.

getMap()->insert(std::make_pair(s, &createT<t>));
</t>


It can be compiled. I include everything it needs.
Thanks
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900