可能一个编译器优化一个常量参数到常量值的引用吗? [英] May a compiler optimize a reference to constant parameter to constant value?

查看:118
本文介绍了可能一个编译器优化一个常量参数到常量值的引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下函数:

void func(const char & input){
 //do something
}

显然,参数是常数值不是
引用关于字符类型的大小,现在可以编译器优化到常量
值,以便它将与以下一样?

Apparently it makes sense for the parameter to be constant value not reference to constant regarding size of the char type, Now may a compiler optimize that to constant value so that it'll be the same as following ?

void func(const char input){
 //do something
}


推荐答案

像某人说的,但是悲伤downvoted(不知道为什么他删除了他的答案),编译器可以做任何事情,只要可观察的行为就像没有做任何不同的事情一样。

Like someone stated, but was sadly downvoted (not sure why he did delete his answer), the compiler can do any and everything as long as the observable behavior is the same as if it did not do anything different.

这是自扩展的,如果你的函数写入引用,并且一个全局变量作为参数传递给函数,并且函数后面打印全局,或任何其他奇怪的事情,然后如果编译器将更改参数传递约定,编译器更难以证明你仍然获得相同的可观察行为。如果编译器不能证明它,它不能做所需的优化。

It's self-expanatory that if your function writes into the reference, and a global variable was passed as argument to the function and the global was later printed after the function returns, or anything else fancy is done, then if the compiler would change the parameter passing convention, it's more difficult for the compiler to prove you still get the same observable behavior. If the compiler can't prove it, it can't do the desired optimization.

因此,无论出现什么问题,只要只要我不会注意到它就可以做任何事情。

So whatever further question comes up, just think "it can do anything as long as I won't notice it".

这篇关于可能一个编译器优化一个常量参数到常量值的引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆