Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的模板专门化检查不足以正确处理相应的类型?_C++_Templates_Casting_Template Specialization - Fatal编程技术网

C++ 为什么我的模板专门化检查不足以正确处理相应的类型?

C++ 为什么我的模板专门化检查不足以正确处理相应的类型?,c++,templates,casting,template-specialization,C++,Templates,Casting,Template Specialization,上下文:我有一个属性包,它有接受int、bool和string属性的公共方法。它们都调用一个模板私有函数,其代码片段附在下面 问题:我正在检查模板专门化以将属性添加到相应的映射中。但代码为C2440的编译器错误“无法从字符串转换为int”和C2440“无法从字符串转换为bool”。我不知道为什么会有这个问题 试图解决: 我尝试了静态强制转换,但它似乎有一个错误,因为它无法从字符串转换为int(或bool!) 模板 void PropertyBag::Add(常量字符串和名称,常量T值) { 字符

上下文:我有一个属性包,它有接受int、bool和string属性的公共方法。它们都调用一个模板私有函数,其代码片段附在下面

问题:我正在检查模板专门化以将属性添加到相应的映射中。但代码为C2440的编译器错误“无法从字符串转换为int”和C2440“无法从字符串转换为bool”。我不知道为什么会有这个问题

试图解决: 我尝试了静态强制转换,但它似乎有一个错误,因为它无法从字符串转换为int(或bool!)

模板
void PropertyBag::Add(常量字符串和名称,常量T值)
{
字符串错误消息;
如果(!IsValidPropertyName(名称,错误消息))
{
m_pFailureHandler->Handle(错误消息);
返回;
}
如果(!IsPropertyNameUnique(名称、m_内容))
{
m_pFailureHandler->Handle(“属性“+”名称“+”不唯一”);
返回;
}
如果(相同::值)
m_content.intProperties[name]=值;
else if(相同::值)
m_content.stringProperties[名称]=值;
else if(相同::值)
m_content.boolProperties[name]=值;
其他的
m_pFailureHandler->Handle(“不支持的值类型”);
}

您的问题是,等到函数体进行测试时,整个函数体必须能够编译,它不会删减无法运行的if语句体

编写一个只不过是映射添加的函数:

void add(std::string const & name, bool value)
{
m_content.boolProperties[name] = value;
}
// And one for others
void add(std::string const &, ...)
{
// failure
}

您的问题是,您要等到函数体进行测试,整个函数体必须能够编译,它不会删减无法运行的if语句体

编写一个只不过是映射添加的函数:

void add(std::string const & name, bool value)
{
m_content.boolProperties[name] = value;
}
// And one for others
void add(std::string const &, ...)
{
// failure
}

如果你被前11C++所困扰,你可以使用RealTytCask.这应该是一个安全的石膏,因为你刚刚检查了它

if (is_same<T, int32_t>::value)
    m_content.intProperties[name] = *reinterpret_cast<const int32_t*>(&value);
// etc...
如果不能更改内容,可以创建免费函数

   void setproperty(Content& content, const std::string& name, bool b)
   {
       content.boolProperties[name] = b;
   }

   void setproperty(Content& content, const std::string& name, int32_t n)
   {
       content.intProperties[name] = n;
   }

   void setproperty(Content& content, const std::string& name, const std::string& s)
   {
       content.stringProperties[name] = s;
   }
然后,您的add函数变为:

template <typename T>
void PropertyBag::Add(const string& name, const T value)
{
    string errorMessage;
    if (!IsValidPropertyName(name, errorMessage))
    {
        m_pFailureHandler->Handle(errorMessage);
        return;
    }

    if (!IsPropertyNameUnique(name, m_content))
    {
        m_pFailureHandler->Handle("Property '" + name + "' is not unique");
        return;
    }
    setProperty(m_content, name, value);        
    // or m_content.setProperty(name, value);
}
模板
void PropertyBag::Add(常量字符串和名称,常量T值)
{
字符串错误消息;
如果(!IsValidPropertyName(名称,错误消息))
{
m_pFailureHandler->Handle(错误消息);
返回;
}
如果(!IsPropertyNameUnique(名称、m_内容))
{
m_pFailureHandler->Handle(“属性“+”名称“+”不唯一”);
返回;
}
setProperty(m_内容、名称、值);
//或m_content.setProperty(名称、值);
}

<代码> > p>如果你被前11C++所困扰,你可以使用RealTytCask.这应该是一个安全的石膏,因为你刚刚检查了它

if (is_same<T, int32_t>::value)
    m_content.intProperties[name] = *reinterpret_cast<const int32_t*>(&value);
// etc...
如果不能更改内容,可以创建免费函数

   void setproperty(Content& content, const std::string& name, bool b)
   {
       content.boolProperties[name] = b;
   }

   void setproperty(Content& content, const std::string& name, int32_t n)
   {
       content.intProperties[name] = n;
   }

   void setproperty(Content& content, const std::string& name, const std::string& s)
   {
       content.stringProperties[name] = s;
   }
然后,您的add函数变为:

template <typename T>
void PropertyBag::Add(const string& name, const T value)
{
    string errorMessage;
    if (!IsValidPropertyName(name, errorMessage))
    {
        m_pFailureHandler->Handle(errorMessage);
        return;
    }

    if (!IsPropertyNameUnique(name, m_content))
    {
        m_pFailureHandler->Handle("Property '" + name + "' is not unique");
        return;
    }
    setProperty(m_content, name, value);        
    // or m_content.setProperty(name, value);
}
模板
void PropertyBag::Add(常量字符串和名称,常量T值)
{
字符串错误消息;
如果(!IsValidPropertyName(名称,错误消息))
{
m_pFailureHandler->Handle(错误消息);
返回;
}
如果(!IsPropertyNameUnique(名称、m_内容))
{
m_pFailureHandler->Handle(“属性“+”名称“+”不唯一”);
返回;
}
setProperty(m_内容、名称、值);
//或m_content.setProperty(名称、值);
}

使用“如果是CONExPR”()而不是常规的“IF”类型检查(即ISHI……等),如果你被前11C++所困扰,你可以使用RealTytQuasCask。这应该是一个安全的强制转换,因为您刚刚检查过。使用“如果CONEXPRPR”()而不是常规的“如果”,类型检查(即ISSH……等等),如果你被卡在前11C++,你可以使用RealTytQuasCube。这应该是一个安全的强制转换,因为您刚刚检查了它。使用
reinepret\u cast
,您必须获取
value
的地址使用
reinepret\u cast
,您必须获取
value