Tag: C#的

何时使用std :: forward来转发参数?

C ++ 0x显示了一个使用std::forward的例子: template<class T> void foo(T&& arg) { bar(std::forward<T>(arg)); } 什么时候使用std::forward是有利的,总是? 另外,它需要在参数声明中使用&& ,在所有情况下都有效吗? 我以为你必须将临时函数传递给一个函数,如果函数是用&&声明的话,那么可以用任何参数调用函数吗? 最后,如果我有这样的函数调用: template<int val, typename… Params> void doSomething(Params… args) { doSomethingElse<val, Params…>(args…); } 我应该用这个来代替: template<int val, typename… Params> void doSomething(Params&&… args) { doSomethingElse<val, Params…>(std::forward<Params>(args)…); } 另外,如果在函数中使用两次参数,即同时转发两个函数,使用std::forward是否明智? 不会std::forward同样的东西临时两次,移动内存,使第二次使用无效? 下面的代码是可以的: template<int val, typename… Params> void doSomething(Params&&… args) { doSomethingElse<val, Params…>(std::forward<Params>(args)…); doSomethingWeird<val, Params…>(std::forward<Params>(args)…); } […]

我应该添加.vcxproj.filter文件到源代码pipe理?

在评估Visual Studio 2010 Beta 2时,我看到在转换后的目录中,我的vcproj文件变成了vcxproj文件。 每个项目旁边还有一些vcxproj.filter文件,它们似乎包含文件夹结构(\ Source Files,\ Header Files等)的描述。 你认为这些过滤文件应该保存在每个用户,还是应该在整个开发组共享,并签入SCC? 我目前的想法是检查他们,但是我想知道是否有什么理由不这样做,或者我应该检查他们的很好的理由。 显而易见的好处是,如果我正在查看别人的机器,那么文件夹结构将会匹配,但也许他们想要逻辑地重组事物?

如何从C ++的void函数中退出?

如果它是一个无效函数,如何过早退出函数? 我有一个无效的方法,需要不执行其代码,如果一定的条件是真的。 我真的不想改变方法来实际返回一个值。

unit testing一个事件在C#中引发

我有一些引发PropertyChanged事件的代码,我希望能够unit testing事件正在提出正确。 引发事件的代码就像 public class MyClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public string MyProperty { set { if (_myProperty != value) { _myProperty = value; NotifyPropertyChanged("MyProperty"); } } } } 我在我的unit testing中使用委托,从下面的代码中得到一个很好的绿色testing: [TestMethod] public void Test_ThatMyEventIsRaised() { string […]

C# – 获取exception的行号

在catch块中,我怎样才能得到抛出exception的行号?

切换if-else语句的优点

使用switch语句的最佳做法是使用30个unsigned枚举的if语句,其中大约10个具有预期的操作(目前是相同的操作)。 性能和空间需要考虑,但并不重要。 我已经抽象了这个片段,所以不要因为命名惯例而恨我。 switch语句: // numError is an error enumeration type, with 0 being the non-error case // fire_special_event() is a stub method for the shared processing switch (numError) { case ERROR_01 : // intentional fall-through case ERROR_07 : // intentional fall-through case ERROR_0A : // intentional fall-through case ERROR_10 : // intentional fall-through case […]

无法理解这种方式来计算一个数字的平方

我find了一个函数来计算一个数字的平方: int p(int n) { int a[n]; //works on C99 and above return (&a)[n] – a; } 它返回n 2的值。 问题是,它是如何做到的? 经过一些testing,我发现(&a)[k]和(&a)[k+1]是sizeof(a) / sizeof(int) 。 这是为什么?

如何将具有相同types项目的列表合并到单个项目列表中?

这个问题令人困惑,但是如下面的代码所描述的那样更加清楚: List<List<T>> listOfList; // add three lists of List<T> to listOfList, for example /* listOfList = new { { 1, 2, 3}, // list 1 of 1, 3, and 3 { 4, 5, 6}, // list 2 { 7, 8, 9} // list 3 }; */ List<T> list = null; // how to merger all […]

为什么C函数不能被名字打乱?

我最近接受了一个采访,有一个问题是在C ++代码中使用了extern "C" 。 我回答说在C ++代码中使用C函数是因为C不使用名称修饰。 有人问我为什么C不使用名称修改,老实说我无法回答。 我明白,当C ++编译器编译函数时,会给函数一个特殊的名字,主要是因为我们可以在C ++中重载相同名称的函数,而这些函数必须在编译时parsing。 在C中,函数的名称将保持不变,或者在_之前。 我的查询是:允许C ++编译器破坏C函数还有什么错? 我会假定编译器赋予它们的名称并不重要。 我们在C和C ++中以相同的方式调用函数。

为什么要在C中写1000,000,000 1000 * 1000 * 1000?

在Apple创build的代码中,有这样一行: CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 ) 有没有任何理由将1,000,000,000 1000*1000*1000 ? 为什么不是1000^3呢?