当我编写一个简单的(非模板)类时,如果函数实现是"正确"提供的,它会自动被视为inline.
class A {
void InlinedFunction() { int a = 0; }
// ^^^^ the same as 'inline void InlinedFunction'
}
Run Code Online (Sandbox Code Playgroud)
在谈论基于模板的类时,这条规则怎么样?
template <typename T> class B {
void DontKnowFunction() { T a = 0; }
// Will this function be treated as inline when the compiler
// instantiates the template?
};
Run Code Online (Sandbox Code Playgroud)
此外,该inline规则如何应用于非嵌套模板函数,例如
template <typename T> void B::DontKnowFunction() { T a = 0; }
template <typename T> inline void B::DontKnowFunction() { T a = 0; } …Run Code Online (Sandbox Code Playgroud) 在C++中,我可以声明一个方法"内联",编译器可能会内联它.据我所知,Java中没有这样的关键字.
如果JVM决定这样做,内联就完成了吗?我能以某种方式影响这个决定吗?
我希望我能恰当地提出问题的标题.
在c#中,我可以使用lambdas(作为委托),或者使用较旧的委托语法来执行此操作:
Func<string> fnHello = () => "hello";
Console.WriteLine(fnHello());
Func<string> fnHello2 = delegate()
{
return "hello 2";
};
Console.WriteLine(fnHello2());
Run Code Online (Sandbox Code Playgroud)
那么为什么我不能"内联"lambda或委托体,并避免在命名变量中捕获它(使其匿名)?
// Inline anonymous lambda not allowed
Console.WriteLine(
(() => "hello inline lambda")()
);
// Inline anonymous delegate not allowed
Console.WriteLine(
(delegate() { return "hello inline delegate"; })()
);
Run Code Online (Sandbox Code Playgroud)
在javascript中工作的示例(仅用于比较)是:
alert(
(function(){ return "hello inline anonymous function from javascript"; })()
);
Run Code Online (Sandbox Code Playgroud)
这会产生预期的警报框.
更新:看起来你可以在C#中使用内联匿名lambda,如果你适当地进行转换,但是()的数量开始让它变得难以驾驭.
// Inline anonymous lambda with appropriate cast IS allowed
Console.WriteLine(
((Func<string>)(() => "hello inline anonymous …Run Code Online (Sandbox Code Playgroud) 正如这个问题所示: 使用扩展方法提升C#事件 - 这很糟糕吗?
我正在考虑使用此扩展方法来安全地引发事件:
public static void SafeRaise(this EventHandler handler, object sender, EventArgs e)
{
if (handler != null)
handler(sender, e);
}
Run Code Online (Sandbox Code Playgroud)
但是Mike Rosenblum在Jon Skeet的回答中提出了这个问题:
你们需要将[MethodImpl(MethodImplOptions.NoInlining)]属性添加到这些扩展方法中,否则你可以通过JITter优化将代理复制到临时变量的尝试,从而允许空引用异常.
我在发布模式下做了一些测试,看看当扩展方法没有用NoInlining标记时是否可以获得竞争条件:
int n;
EventHandler myListener = (sender, e) => { n = 1; };
EventHandler myEvent = null;
Thread t1 = new Thread(() =>
{
while (true)
{
//This could cause a NullReferenceException
//In fact it will only cause an exception in:
// debug x86, debug x64 and release x86
//why doesn't it …Run Code Online (Sandbox Code Playgroud) ISO C++表示C++中成员函数的内联定义与使用内联声明它的相同.这意味着将在使用成员函数的每个编译单元中定义该函数.但是,如果函数调用因任何原因无法内联,则该函数将"照常"实例化.(http://msdn.microsoft.com/en-us/library/z8y1yy88%28VS.71%29.aspx我对这个定义的问题在于它没有告诉它将被实例化到哪个翻译单元.我遇到的问题是,当在一个静态库中面对两个目标文件时,这两个目标文件都引用了一些无法内联的内联成员函数,链接器可能会"选择"任意对象文件作为定义的源.这种特殊选择可能会引入不必要的依赖关系.(除其他事项外)
例如: 在静态库中
A.h:
class A{
public:
virtual bool foo() { return true; }
};
Run Code Online (Sandbox Code Playgroud)
U1.cpp:
A a1;
Run Code Online (Sandbox Code Playgroud)
U2.cpp:
A a2;
Run Code Online (Sandbox Code Playgroud)
和许多依赖
在另一个项目 main.cpp中:
#include "A.h"
int main(){
A a;
a.foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第二个项目是第一个.我如何知道编译器将使用哪个定义,以及哪些目标文件与其依赖关系将被链接?那个标准上有什么标准吗?(试过,但未能找到)
谢谢
编辑:因为我看到有些人误解了问题所在,我想强调:如果编译器决定为该函数创建一个符号(在这种情况下,由于'虚拟',它将会是不同目标文件中的几个(外部看到的)实例化,链接器选择哪个定义(从哪个目标文件?)?)
运算符和其他方法在C++中进行内联是否有任何区别?我已经搜索过它,但这不是一个常见的问题,正如我所看到的那样.有没有人有充分的理由使用它或避免?注意:显然,我的意思是内联运算符,当它们很小时.
我编写了一个简单的SessionItem管理类来处理所有那些讨厌的空检查,如果不存在则插入一个默认值.这是我的GetItem方法:
public static T GetItem<T>(string key, Func<T> defaultValue)
{
if (HttpContext.Current.Session[key] == null)
{
HttpContext.Current.Session[key] = defaultValue.Invoke();
}
return (T)HttpContext.Current.Session[key];
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何实际使用它,将Func <T>作为内联方法参数传递?
我没想到肯定是或否.你可能拥有的任何知识我都会考虑作为答案.
private String CalculateCharge(Nullable<Decimal> bill, Nullable<Decimal> rate)
{
return ((bill ?? 0.0m) * (rate ?? 0.0m)).ToString("C");
}
Run Code Online (Sandbox Code Playgroud) 为什么在ActionScript中很少使用内联闭包?它们非常强大,我认为非常可读.我几乎没有看到有人使用它们,所以也许我只是在查看错误的代码.谷歌在他们的Google Maps API for Flash示例中使用它们,但我认为这是我见过它们的唯一地方.
我喜欢它们,因为您可以访问定义它们的作用域中的局部变量,并且您将逻辑保留在一个方法中,并且最终不会有许多函数,您必须为它们提供一个名称.
有没有使用它们?它们的工作方式与C#中的工作方式大致相同.
我实际上只是发现AS3支持它们,我很生气因为我以为我读过他们在AS#中被弃用了.所以我回来使用它们了!
private function showPanel(index:int):void {
_timer = new Timer(1000, 1);
_timer.addEventListener(TimerEvent.TIMER, function(event:Event):void
{
// show the next panel
showPanel(index++);
});
Run Code Online (Sandbox Code Playgroud) " 使用内联函数有什么问题 "和" 递归函数可以内联 "是否适用于Delphi内联函数?此外,有谁知道如何在Delphi中处理递归内联函数?
我有一些技术问题,我在工作中重复了代码,我想摆脱它,所以我知道在C++中使用宏不是一个好主意,而是我必须使用内联函数,这是个好主意将此函数用作内联:
list<Data>::iterator foo(int data){
if(dataExists(data)){
list<Data>::iterator i;
for(i = dataClass.begin(); i != dataClass.end(); ++i){
if(i->getData() == data){
break;
}
return i; //here I have one more problem, what can I return if data doesn't exist?
}
Run Code Online (Sandbox Code Playgroud)
我是初学者,我认为这个功能非常不安全,有人可以给我建议,我怎样才能改进我的代码,提前谢谢
PS通常用什么来避免重复代码?
inline-method ×12
c++ ×4
c# ×3
.net ×1
actionscript ×1
c ×1
delphi ×1
delphi-2009 ×1
func ×1
generics ×1
heuristics ×1
inlining ×1
java ×1
jit ×1
jvm ×1
lambda ×1
operators ×1
recursion ×1
templates ×1