内联
CLion 提供以下内联重构:
内联常量。 此重构与 提取常量 重构相反。
内联定义。 此重构与 提取 define 重构相反。
内联类型定义。 此重构与 提取 typedef 重构相反。
内联参数。 此重构与 提取参数 重构相反。
内联函数。 此重构与 提取函数 重构相反。
示例
之前 | 之后 |
|---|---|
//This function will be inlined
int fdiff(int x, int y, int factor);
int main(){
int x = 10;
int y = 9;
int z = fdiff(x, y, 2);
return 0;
}
int fdiff(int x, int y, int factor) {
return (x-y) * factor;
}
|
int main(){
int x = 10;
int y = 9;
int z = (x - y) * 2;
return 0;
}
|
之前 | 之后 |
|---|---|
int main (int argc, const char * argv[]) {
@autoreleasepool {
float result;
//The third parameter will be inlined
result = mulfunc(10, 20, 2);
}
return 0;
}
float mulfunc(int x, int y, int factor) {
return x * y * factor;
}
|
int main (int argc, const char * argv[]) {
@autoreleasepool {
float result;
result = mulfunc(10, 20);
}
return 0;
}
float mulfunc(int x, int y) {
return x * y * 2;
}
|
之前 | 之后 |
|---|---|
import math
class Solver:
def demo(self):
a = 3
b = 25
c = 46
#This variable will be inlined
return_type_of_sqrt = math.sqrt(b ** 2 - 4 * a * c)
root1 = (-b + return_type_of_sqrt) / (2*a)
root2 = (-b - return_type_of_sqrt) / (2*a)
print(root1,root2)
Solver().demo()
|
import math
class Solver:
def demo(self):
a = 3
b = 25
c = 46
root1 = (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
root2 = (-b - math.sqrt(b**2 - 4*a*c)) / (2*a)
print(root1,root2)
Solver().demo()
|
之前 | 之后 |
|---|---|
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
//This variable will be inlined
var clen = document.cookie.length;
var i = 0;
while (i != clen) {
var j = i + clen;
...
}
}
|
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var i = 0;
while (i != document.cookie.length) {
var j = i + document.cookie.length;
...
}
}
|
执行内联重构
将插入符号放在要内联的目标符号处。
请执行以下操作之一:
从主菜单或上下文菜单中选择 。
按下 Ctrl+Alt+N。
在与所选符号对应的 内联 对话框中,确认内联重构或在 查找重构预览 中查看所选符号的用法。
最后修改日期: 2025年 9月 26日