将 int 转换为枚举
我正在尝试在 C++/CLI 帮助程序类中使用通用枚举类型,并且如果强制转换不起作用,我希望它默认为 0。我的问题是 result = (T)0;
不起作用。有办法解决这个问题吗?
错误 1 错误 C2440:“类型转换”:无法从“int”转换为“T”
generic <typename T> where T: value class, Enum
static void SetEnumPropertyValue(String^ value, [Out] T %result)
{
if (String::IsNullOrEmpty(value))
false;
if (!Enum::TryParse<T>(value, true, result))
{
result = (T)0;
}
}
I'm trying to use a generic enum type in a C++/CLI helper class and I want it to default to 0 if the cast doesn't work. My problem is that result = (T)0;
doesn't work. Is there a way around this?
Error 1 error C2440: 'type cast' : cannot convert from 'int' to 'T'
generic <typename T> where T: value class, Enum
static void SetEnumPropertyValue(String^ value, [Out] T %result)
{
if (String::IsNullOrEmpty(value))
false;
if (!Enum::TryParse<T>(value, true, result))
{
result = (T)0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要么使用:
要么稍微“丑陋”:
Either use:
or the slightly "uglier":