我有一些模板代码适用于 Xcode 4.5 和 LLVM 3.0,但适用于 VS 2010 Express C++ 工具链 (v 10.0.30319.1)。
我正在使用我无法控制的第三方 API。它以只能由 API 函数解释的黑盒“blob”形式为我的代码提供值:
// API_Secret is a black-box encapsulation of a floating-point number or a boolean value.
// It is provided by a third-party API, with associated access functions.
// For all intents and purposes, it's a complete black box.
// This enum represents the internal 'type' of a secret value.
enum API_SecretTypeEnum {
API_Number,
API_Boolean,
};
// Other API declarations:
API_SecretTypeEnum API_GetType(const API_Secret &value);
double API_AsNumber(const API_Secret &value);
bool API_AsBoolean(const API_Secret &value);
// my code:
template <typename ValueType>
class Extractor {
public:
ValueType extract(API_Secret value) {
if (API_GetType(value) == API_Number) {
return static_cast<ValueType>(API_AsNumber(value));
} else if (API_GetType(value) == API_Boolean) {
return API_AsBoolean(value) ? ValueType(1) : ValueType(0);
}
return ValueType();
}
};
// boolean specialization - not 100% sure it's needed though
template<>
class Extractor <bool> {
public:
bool extract(API_Secret value) {
return API_AsBoolean(value);
}
};
然后,稍后:
API_Secret API_GetSomeValue(int some_sort_of_handle);
// get some black-box values from the API
API_Secret secret0 = API_GetSomeValue(0);
API_Secret secret1 = API_GetSomeValue(1);
API_Secret secret2 = API_GetSomeValue(2);
// for certain external reasons we expect this to be an integer representation:
Extractor<int> e0;
int v0 = e0.extract(secret0);
// for certain external reasons we expect this to be a double representation:
Extractor<double> e1;
double v1 = e1.extract(secret1);
// for certain external reasons we expect this to be a boolean representation:
Extractor<bool> e2;
bool v2 = e2.extract(secret2);
现在了解 Xcode、LLVM 和 VS 2010 之间的区别。在 Xcode 和 LLVM 中,将编译以下内容(作为完整程序的一部分):
enum MyEnum {
Enum0,
Enum1,
};
Extractor<MyEnum> ee;
MyEnum ve = ee.extract(secret0);
即类模板使用 static_cast 将 float 转换为枚举。这似乎工作正常,this page 的解释部分表明这是有效的:
8) Integer, floating-point, or enumeration type can be converted to any enumeration type (the result is unspecified if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
但是使用VS2010,会遇到如下编译错误:
error C2440: 'static_cast' : cannot convert from 'double' to 'MyEnum'
Conversions between enumeration and floating point values are no longer allowed
还有这个MSDN article似乎通过不提及浮点类型并明确声明“整数”值来证实这一点:
The static_cast operator can explicitly convert an integral value to an enumeration type. If the value of the integral type does not fall within the range of enumeration values, the resulting enumeration value is undefined.
因此,VS 2010 和其他编译器之间似乎存在显着差异。我想知道这是否可以在 VS 2010 中绕过?它是 VS 2010 根本不支持的语言的新功能吗?但是我对此进行了查询,因为错误消息显示“不再允许”,这意味着它已被明确禁止。
我知道一个解决方法 - 我可以使用 Extractor<MyEnum> 而不是转换为枚举(使用 Extractor<int> )相反,然后简单地将其分配给目标 MyEnum 变量。然而,这个模板实际上用作调用包装函数的更大系统的一部分,在这种情况下,包装函数采用 MyEnum 值。这会阻止模板正确匹配,因为 ValueType 参数实际上是从包装函数的签名中自动获取的。
或者,是否可以编写一个匹配 enum 的 Extractor 模板特化?只有类型?然后我可以先转换为整数类型。或者也许基本模板总是可以首先转换为 int,然后我可以编写一个不这样做的浮点特化 - 但我不确定如何编写一个捕获所有浮点类型的模板(float,双,...)。
最佳答案
我很确定Visual Studio-2010 supports <type_traits> .您可以使用 std::enable_if连同 std::is_enum .
template <typename ValueType, typename Enable = void>
class Extractor {
public:
ValueType extract(API_Secret value);
};
template <typename ValueType>
class Extractor<ValueType, typename std::enable_if<std::is_enum<ValueType>::value>::type>
{
...
};
您可以使用 std::is_floating_point 来匹配浮点类型。 .
关于C++ 将浮点值转换为枚举 - 但不是 VS 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14821846/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候