1

我确实有一个小问题,我想解决。

使用类模板,我使用 typeid() 检查数据类型,以便以正确的方式处理它们。


  template <typename _Ty_Id,
            typename _Ty_Value>
  class Attribute : public AttributeBase<_Ty_Id>
  {
    public: 
      Attribute() : 
        AttributeBase(_Ty_Id()),
        m_tyValue(_Ty_Value())
      {
      }

      Attribute(_Ty_Id tyId, _Ty_Value tyValue) : 
        AttributeBase(tyId),
        m_tyValue(tyValue)
      {
      }

      virtual ~Attribute() {};

      virtual  _Ty_Id    getId()    const {return m_tyId;}
      virtual  _Ty_Value getValue() const {return m_tyValue;}

      virtual void toXml(iolib::Xml& xmlFile)
      {
        std::list<std::string> listXmlData;
        std::string strTag;

        if(typeid(m_tyId) == typeid(std::string))
        {
          strTag = m_tyId;
        }
        else
        {
          strTag = core::Stringfunc::format(strTag, m_tyId, 0, 0);
        }

        listXmlData.push_back(strTag);
        listXmlData.push_back("entrytype");
        listXmlData.push_back(m_strEntryType);
        listXmlData.push_back("datatype");
        listXmlData.push_back(m_strDataType);

        std::string strValue;
        if(typeid(m_tyValue) == typeid(std::string))
        {
          #pragma warning(disable : 4244)
          strValue = m_tyValue;
          #pragma warning(default : 4244)
        }
        else if((typeid(m_tyValue) == typeid(float)) || 
                (typeid(m_tyValue) == typeid(double)))
        {
          strValue = core::Stringfunc::format(strValue, m_tyValue, 0, 3);
        }
        else
        {
          strValue = core::Stringfunc::format(strValue, m_tyValue, 0, 0);
        }

        listXmlData.push_back(strValue);
        listXmlData.push_back("/" + strTag);

        xmlFile.addElement(listXmlData);
      }


    private:
      _Ty_Value m_tyValue;
  }

模板参数是一个浮点值,将在 else if 分支中处理。但是,VS 2010 会带来警告 #4244,即从 float 到 std::string 的隐式转换可能无法正常工作。

到目前为止,我在它发生的地方禁用了该警告,一切都会好起来的。但是,这当然只适用于 VS2012,并且希望将我的代码用于不同的目标。

有谁知道防止警告的更聪明的方法?


编辑

所以,现在我确实发布了课程的主要部分。

The problem is within the method: toXml(iolib::Xml& xmlFile), with will accept a referenct to my xml class. The used type (m_tyValue) is a member of the Attribute<> class.

The template arguments are: std::string for m_tyId and float for m_tyValue.

Best regards, Coast

4

1 回答 1

0

Note: you should not use names beginning with __ (double underscore) or with _ and an uppercase letter. Those are reserved "for the implementation" [global.names] in the C++ Std

To get different behaviour for different types (where you know the type at compile time), you use can use (partial) specialization of the class template:

template < typename Ty_Id >
struct toXmlHelper
{}; // provide no default implementation

template < >
struct toXmlHelper < double > // implementation in case Ty_Id == double
{
    void toXml() { /*...*/ }
};

template < >
struct toXmlHelper < std::string > // implementation in case Ty_Id == std::string
{
    void toXml() { /*...*/ }
};

You might want to add techniques to reduce code redundancy.

Another way would be to use the overloading mechanism:

template < typename Ty_Id,
           typename Ty_Value>
class Attribute : public AttributeBase<_Ty_Id>
{
public:
    /* ... */
    virtual void toXml(iolib::Xml& xmlFile)
    {
        std::list<std::string> listXmlData;
        std::string strTag;
        /* ... */
        toXmlHelper(m_tyId);
    }
private:
    void toXmlHelper(double);
    void toXmlHelper(std::string const&);
}

As you know the type of m_tyId, there's no reason to use Run-Time Type Info. The warning appears as the code (in each if-clase) in compiled for all instantiations of your class template, even if it does not make sense, like assigning a float to a string.

于 2013-04-07T20:14:41.333 回答