gpt4 book ai didi

c++ - 在这些情况下将如何处理推荐人?

转载 作者:行者123 更新时间:2023-11-30 03:09:12 25 4
gpt4 key购买 nike

我知道当引用是函数的参数并且该函数是内联的时,那么引用可能是引用本身而不一定是指向它的指针,但是当引用不是参数时呢?但对于函数是局部的或全局的,或者是内联函数的输出:

//global scope

void someFunc(SomeType & ref){//when function is inline, it's possible for ref to be the referent itself
//function body
}
int num=7;
int & ref=num;//what about ref here?
void someFunc1(){
int num=6;
int & ref=num;//what about ref here?
//rest of function body
}
int & someFunc2(){//what about output reference here when function is inlined, will it be num itself or a pointer ?
int num=8;
return num;
}

最佳答案

David Rodríguez - dribeas 已经在评论中指出,该标准在涉及引用时为编译器提供了相当大的自由度。它们可能会或可能不会占用空间;它们本身不是合适的对象,等等。

您描述的引用类型函数参数的行为(在内联期间消除)对于编译器在引用时所拥有的自由来说是典型的。但这是一种自由,而不是他们的义务。这也可能并不总是可能的:当您调用 SomeFunc( a>5 ? foo : bar); 时,编译器无法将引用替换为“the”所指对象本身。

您的其他示例同样不受约束。理论上,可以优化全局和局部引用,因为没有什么可以阻止它。最后一个示例可以完全内联为 exit(NasalDemons());,因为您要返回对超出范围的对象的引用。

关于c++ - 在这些情况下将如何处理推荐人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4323023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com