这是对我之前的问题 The address of a function matching a bool vs const void* overload 的切线跟进.回答者解释说:
The [C++11] standard does not define any standard conversions from a "pointer to function" to a "pointer to
void."It's hard to provide a quote for the absence of something, but the closest I can do is C++11 4.10/2 [conv.ptr]:
A prvalue of type “pointer to cv
T,” whereTis an object type, can be converted to a prvalue of type “pointer to cvvoid”. The result of converting a “pointer to cvT” to a “pointer to cvvoid” points to the start of the storage location where the object of typeTresides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject). The null pointer value is converted to the null pointer value of the destination type.(强调我的)
假设 func被宣布为 void func(); , 如果你进行 C 风格的转换,即 (void*) func , 施法就会成功。 static_cast<void*>(func)然而无效,但 reinterpret_cast<void*>(func)会成功的。但是,您不能做的是将随后转换的指针转换回其原始类型。例如,
很好:
int main() {
int* i;
void* s = static_cast<void*>(i);
i = static_cast<int*>(s);
s = reinterpret_cast<void*>(i);
i = reinterpret_cast<int*>(s);
}
不好:
void func() { }
int main() {
void* s = reinterpret_cast<void*>(func);
reinterpret_cast<decltype(func)>(s);
}
N3337首先说,
[expr.reinterpret.cast]
The result of the expression
reinterpret_cast<T>(v)is the result of converting the expressionvto typeT. IfTis an lvalue reference type or an rvalue reference to function type, the result is an lvalue; ifTis an rvalue reference to object type, the result is an xvalue; otherwise, the result is a prvalue and the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the expression v. Conversions that can be performed explicitly usingreinterpret_castare listed below. No other conversion can be performed explicitly usingreinterpret_cast.
我将我认为是关键的语言加粗。最后一部分似乎暗示如果转换未列出,则为非法。简而言之,允许的转换是:
X,则“指向 T1 类型的成员的指针”类型的纯右值可以显式转换为不同类型的纯右值“指向 T2 类型的 Y 的成员的指针”和 T1 2 都是函数类型或对象类型。T 类型的左值表达式如果“指向 T1 的指针”类型的表达式可以使用 reinterpret_cast 显式转换为“指向 T2 的指针”类型,则可以将其转换为类型“对 T1 的引用”。 T2不是函数指针,并且对象没有函数或 void 类型。
[基本类型]
An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.
所以也许我正在捕获稻草,但似乎 void*是非法的。然而,另一方面,[expr.static.cast]/5 说“否则,reinterpret_cast<void*>(func) 将执行下列转换之一。不得进行其他转换
使用 static_cast 显式执行.”的关键区别是“shall”和“can”。这足以使static_cast合法还是我错过了其他东西?
最佳答案
(所有引号都来自 N3337,并且对于从那里开始的 N4296 之前的每个草稿都是等效的,即此答案至少对 C++11 和 C++14 有效 但不适用于 C+ +03 因为该答案的第一个引号不存在。)
[expr.reinterpret.cast]/8:
Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv-qualification, shall yield the orignal pointer value.
这包含在您的列表中。您认为 void 不是对象类型,但您没有考虑到关键的 [basic.compound]/3:
The type of a pointer to
voidor a pointer to an object type is called an object pointer type.
(也就是说,对象指针类型不一定是“指向对象类型的指针”——标准术语让你明白了。)
唯一的原因
f = reinterpret_cast<decltype(f)>(s);
在 GCC 或 Clang 上不好的是目标类型(与源表达式相反)是 not 衰减的 - 而且您显然不能强制转换 void*为函数类型。您需要将目标类型设为函数指针 then it works .
关于c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27229578/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po