鉴于以下内容,为什么会抛出InvalidCastException?我不明白为什么它应该在一个bug之外(这是在x86; x64与clrjit.dll中的0xC0000005崩溃).
class Program
{
static void Main(string[] args)
{
MyDouble? my = new MyDouble(1.0);
Boolean compare = my == 0.0;
}
struct MyDouble
{
Double? _value;
public MyDouble(Double value)
{
_value = value;
}
public static implicit operator Double(MyDouble value)
{
if (value._value.HasValue)
{
return value._value.Value;
}
throw new InvalidCastException("MyDouble value cannot convert to System.Double: no value present.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是为以下内容生成的CIL Main():
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 3
.locals init …Run Code Online (Sandbox Code Playgroud) 喜.我正在阅读Digi Traffic Accelerator反编译源(我认为这是最好的学习方法),直到我得到一些不可理解的代码!请看一下:
internal class ProxyFarm
{
private static Random rand = new Random();
private static Regex UserPassAtHostPort = new Regex("\r\n ^\r\n (?<user>[^:]+?) : (?<pass>[^@]+?)\r\n @\r\n (?<host>[^:]+? (?: : \\d+)? )\r\n $", RegexOptions.IgnorePatternWhitespace);
private static Regex HostPortUserPass = new Regex("\r\n ^\r\n (?<host>[^:]+? : \\d+) : (?<user>[^:]+?) : (?<pass>.+?)\r\n $", RegexOptions.IgnorePatternWhitespace);
public const string NEW = "new";
public const string ACTIVE = "active";
public const string BLOCKED_BY_GOOGLE = "blocked by Google";
public const string INACTIVE = "inactive";
public const …Run Code Online (Sandbox Code Playgroud) 如果您编译以下代码:
private async Task<int> M()
{
return await Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)
然后反编译它(我使用dotPeek)并检查所有重要的MoveNext方法,你会看到一个bool在开头附近声明的变量; dotPeek为我选择了"旗帜".
bool flag = true;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您将在启动第一个异步调用后的默认case语句中看到该变量的后续使用者:
if (!awaiter.IsCompleted)
{
this.\u003C\u003E1__state = 0;
this.\u003C\u003Eu__\u0024awaiter11 = awaiter;
this.\u003C\u003Et__builder.AwaitUnsafeOnCompleted<TaskAwaiter<int>, Program.\u003CP\u003Ed__10>(ref awaiter, ref this);
flag = false;
return;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了六个比我最初的例子更复杂的例子,并且它们在退出方法之前只分配给这个变量是一致的.所以换句话说,在我迄今为止尝试过的所有情况中,这个变量不仅从不消耗,而且只是在从方法返回之前立即给出一个非初始值 - 这是一个定义分配的时间点无用.
作为背景,我很享受尝试通过C# - > JS交叉编译器在Javascript中实现async/await的过程.我试图了解在什么情况下我需要考虑这个标志的效用.面对面,它似乎是虚假的,因此我应该忽略它.但是,我想了解为什么C#编译器引入了这个变量 - 我怀疑有更复杂的表达式以有用的方式使用这个变量.
简单来说:为什么C#编译器会生成这个flag变量?
The following code compiles in gcc 9.1 godbolt but not clang 8 godbolt:
class A {
protected:
~A() = default;
};
class B final : public A {
};
int main() {
auto b = B{};
}
Run Code Online (Sandbox Code Playgroud)
Clang's error:
<source>:10:16: error: temporary of type 'A' has protected destructor
auto b = B{};
^
<source>:3:5: note: declared protected here
~A() = default;
^
Run Code Online (Sandbox Code Playgroud)
Which is correct and why?
当我写一堂课 Widget.java
public class Widget {
int data;
String name;
}
Run Code Online (Sandbox Code Playgroud)
编译器生成的构造函数是公共的还是默认的?
公众会是这样的
public class Widget {
int data;
String name;
public Widget() {}
}
Run Code Online (Sandbox Code Playgroud)
而默认类似于
public class Widget {
int data;
String name;
Widget() {}
}
Run Code Online (Sandbox Code Playgroud) 假设我正在编写一个int包装器并且需要提供每个运算符重载。作者必须列出每一个,还是可以根据作者提供的内容自动生成任何一个?编译器是否可以/是否从现有运算符中推断出任何新的自动定义运算符?
如果我定义operator==,它会operator!=自动给我一个吗?或相反亦然?
如果我定义operator++(),我可以operator++(int)免费获得吗?或相反亦然?
怎么样+=类型的企业?它可以结合operator+with 的现有定义operator=来生成operator+=吗?理论上应该是可能的,但有吗?
同样的问题>=to<等,还是我必须完整列出>, >, >=, 的定义<=?
class Base
{
virtual void foo() = 0;
//~Base(); <-- No destructor!
};
Run Code Online (Sandbox Code Playgroud)
显然,Base将得出.那么,C++是否说编译器生成的析构函数Base必须是虚拟的?
谢谢!
偶然发现了一个相对常用的代码,起初看起来效率很低。(我知道优化有时可能是邪恶的,但我想知道)
简介部分-相当简单的SP执行+读取返回的数据:
try
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = sql.ToString();
command.Parameters.AddRange(sqlParameters.ToArray());
var reader = await command.ExecuteReaderAsync();
if (reader.HasRows)
{
while (await reader.ReadAsync())
{
var item = await GetProjectElement(reader);
list.Add(item);
}
}
reader.Dispose();
}
}
finally
{
connection.Close();
}
Run Code Online (Sandbox Code Playgroud)
我担心的是功能
等待GetProjectElement(阅读器)
private async Task<Project> GetProjectElement(DbDataReader reader)
{
var item = new Project
{
Id = await reader.GetFieldValueAsync<int>(1),
ParentId = await reader.IsDBNullAsync(2) ? default(int?) : await reader.GetFieldValueAsync<int>(2),
Name = await reader.IsDBNullAsync(3) ? default(string) : await reader.GetFieldValueAsync<string>(3),
Description …Run Code Online (Sandbox Code Playgroud) 我在我的代码中发现了一个内存泄漏,它是由只调用对象的基类析构函数引起的。这个问题明白了:我已经virtual在接口类的析构函数中添加了MyÌnterface。让我感到困惑的是,编译器显然为我的助手类创建了一个标准的析构函数,MyHelper最终会被调用。我用两个不同的编译器试过这个。
这让我感到非常惊讶,因为我观察到如果成员或基类引入限制,则不会创建大多数默认实现。为什么析构函数的保护不被继承?
#include <iostream>
class MyInterface
{
public:
virtual void doSomethingUseful()=0;
// a lot more functions declared omitted
virtual void doSomethingElse()=0;
virtual void doSomethingIndividual()=0;
protected:
/// protected destructor to forbid calling it on interfaces
~MyInterface() {} // HERE the virtual is clearly missing
};
/// a common base that defaults most functions implementations
class MyHelper: public MyInterface
{
public:
void doSomethingUseful() {}
// a lot more default implementations omitted
void doSomethingElse() {}
}; …Run Code Online (Sandbox Code Playgroud) c# ×4
c++ ×4
destructor ×3
.net ×1
async-await ×1
asynchronous ×1
c++17 ×1
cil ×1
decompiler ×1
dotpeek ×1
inheritance ×1
java ×1
nullable ×1
visibility ×1