有关外部链接和调用约定的C ++标准 [英] C++ Standard regarding external linkage and calling conventions
问题描述
我已经阅读了最新的C ++ 11草案(n3337-它是最后一个吗?),我对正在研究的可能实现方案有疑问.
I've read the last C++11 draft (n3337 - is it the last one?), and I got a question for a possible implementation I've been working on.
假设我们有以下代码:
extern "Objective C" {
class Object {
public:
static Object *alloc();
Object *init();
};
};
然后打电话
Object *x = Object::alloc()->init();
问题是,我不知道编译器是否可以控制外部"X"块的调用约定:这个想法是将调用"翻译"到objc_msgSend(blablabla)-符合该标准,还是将其视为扩展(因为它不仅会修改符号名称,而且还会修改此处的复杂调用约定")?当然,我可以通过创建一个称为__thiscall的弱函数来实现此功能,然后调用该方法并返回该方法本身-但问题仍然存在,是否会保持一致?
The question is that I didn't understand if it is allowed for the compiler to control the calling convention of extern "X" blocks: the idea would be to "translate" the calls to objc_msgSend(blablabla) - would that be conformant to the standard, or would it be considered an extension (since it wouldn't just modify the symbol name, but as well as a complex "calling convention" here)? Of course I could implement this by making a weak function that is called as __thiscall and then calls and returns the method itself - but the question continues, would it be conformant?
谢谢!
推荐答案
您描述的所有声音都符合语言链接功能的意图.
Everything you describe sounds to be in line with the intent of the language linkage feature.
班级成员被明确排除在"C"
语言链接之外.根据标准(C ++ 11§7.5/4)中的示例,类成员声明,任何上下文中的extern
声明以及所有其他函数声明中的函数指针类型均继承了封闭的extern "C" {}
块.您正在定义除"C"
之外的语言链接,但是遵循它的示例可能并不奇怪,也许是在this
和self
之间进行了额外的映射.
Class members are specifically excluded from "C"
language linkage. According to examples in the Standard (C++11 §7.5/4), function pointer types within class member declarations, extern
declarations in any context, and all other function declarations do inherit the enclosing extern "C" {}
block. You're defining a language linkage besides "C"
but it would be least surprising to follow its example, perhaps with extra mapping between this
and self
.
根据§7.5/2,要求是开放的:
The requirements are open, according to §7.5/2:
通过实现定义的语义有条件地支持使用除"C"或"C ++"之外的 string-literal . [注意:因此,实现未知的带有字符串文字的链接规范需要诊断. —尾注] [注:建议从定义该语言的文档中获取字符串字面量的拼写.例如,取决于年份,Ada(不是ADA)和Fortran或FORTRAN. —尾注]
Use of a string-literal other than "C" or "C++" is conditionally-supported, with implementation-defined semantics. [ Note: Therefore, a linkage-specification with a string- literal that is unknown to the implementation requires a diagnostic. — end note ] [ Note: It is recommended that the spelling of the string-literal be taken from the document defining that language. For example, Ada (not ADA) and Fortran or FORTRAN, depending on the vintage. — end note ]
您可以在花括号内切换到完全不同的语言,这样就可以了. (尽管从语法上讲,我认为所有内容都应解析为C ++ 声明或 declaration-seq .)
You could switch to a completely different language within the braces an it would be alright. (Although, grammatically, I suppose everything should parse as a C++ declaration or declaration-seq.)
但是,因此,您应该将extern "Objective-C"
与连字符一起使用,因为最终文档的标题是"The Objective-C Programming Language".
According to that, though, you should use extern "Objective-C"
with a hyphen since the definitive document's title is "The Objective-C Programming Language."
编辑:可以肯定的是,通过objc_msgSend
调用不会影响标准指定的任何内容.它无处说明如何调用C ++函数.添加的中间函数只是机器级别的指令,超出了语言的语义范围,与用于调用Pascal,Fortran等的特殊指令没有什么不同,例如.通过更改堆栈上参数的顺序.我并不是要将您的方案与在花括号内切换为完全不同的语言"进行比较,只是要强调还有足够的余量.
To be sure, calling through objc_msgSend
doesn't affect anything that the Standard specifies. Nowhere does it say how C++ functions are called. The added intermediate function is just machine-level instructions, beyond language semantics, and no different from the special instructions used to call into Pascal, Fortran, etc., e.g. by altering the order of parameters on the stack. I don't mean to compare your scheme to "switching to a completely different language within the braces," just to emphasize that there's plenty of headroom.
以Standard中的extern "C"
规范为例,它从根本上更改了ODR规则,这打破了很多事情,因为不需要C支持任何处理.因此,您的方案至少可以破坏 这么多.
Taking the extern "C"
specification in the Standard as an example, it breaks a lot of things by fundamentally changing the ODR rule because C isn't required to support any mangling. So your scheme is allowed to break at least that much.
这篇关于有关外部链接和调用约定的C ++标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!