我们知道"const变量"表示一旦分配,就无法更改变量,如下所示:
int const i = 1;
i = 2;
Run Code Online (Sandbox Code Playgroud)
上面的程序将无法编译; gcc提示错误:
assignment of read-only variable 'i'
Run Code Online (Sandbox Code Playgroud)
没问题,我能理解,但下面的例子超出了我的理解:
#include<iostream>
using namespace std;
int main()
{
boolalpha(cout);
int const i = 1;
cout << is_const<decltype(i)>::value << endl;
int const &ri = i;
cout << is_const<decltype(ri)>::value << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出
true
false
Run Code Online (Sandbox Code Playgroud)
奇怪的.我们知道,一旦引用绑定到名称/变量,我们就无法更改此绑定,我们更改其绑定对象.所以我认为类型ri应该是相同的i:什么时候i是int const,为什么ri不是const?
我尝试使用windbg在输入成员函数时打印"this"指针,如下所示.
class IBase {
int m_i;
public:
IBase() :m_i(23) {}
virtual int FuncOne(void) = 0;
virtual int FuncTwo(void) = 0;
};
class DerivedOne : public IBase {
public:
virtual int FuncOne(void) { return 1; };//set break point here.
virtual int FuncTwo(void) { return 2; };
};
class DerivedTwo : public IBase {
public:
virtual int FuncOne(void) { return 101; };
virtual int FuncTwo(void) { return 102; };
};
void DoIt(IBase* Base)
{
int i=Base->FuncOne();//break point here
}
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 我正在使用python 2.7和ipython2.7.在ipython中我尝试过:
class Fib(object):
def __init__(self, max):
super(Fib, self).__init__()
self.max = max
def __iter__(self):
self.a = 0
self.b = 1
return self
def __next__(self):
fib = self.a
if fib > self.max:
raise StopIteration
self.a, self.b = self.b, self.a + self.b
return fib
def main():
fib = Fib(100)
for i in fib:
print i
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
那么它报告错误:
类型错误回溯(最近通话最后一个)在()22 23如果名称 == ' 主要 ':---> 24主()25
<ipython-input-21-f10bd2d06666> in main()
18 def main():
19 fib = Fib(100)
---> 20 for …Run Code Online (Sandbox Code Playgroud) 我注意到在使用C#构建一个较旧的MVC项目时,有很多对OWIN库的引用.但是在VS2015 SP3中创建.NET Core Web应用程序时,似乎没有对它的引用.
这个图书馆已经过时了吗?
我得到一个矩阵
>>> matrix=[[1, 2, 3], [4, 5, 6]]
Run Code Online (Sandbox Code Playgroud)
我可以用它转置:
>>> zip(*matrix)
[(1, 4), (2, 5), (3, 6)]
Run Code Online (Sandbox Code Playgroud)
我试图在没有它的情况下使用,但失败了:
>>> zip(matrix)
[([1, 2, 3],), ([4, 5, 6],)]
Run Code Online (Sandbox Code Playgroud)
"*"在这里是什么意思?
我使用 vs 2019 作为控制台应用程序(dotnet core 3.1),
Install-Package Microsoft.Extensions.Logging.Console -Version 3.1.2
Run Code Online (Sandbox Code Playgroud)
和这段代码:
using Microsoft.Extensions.Logging;
class Program
{
static void Main(string[] args)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Information)
.AddFilter("System", LogLevel.Information)
.AddFilter("TestRx.Program", LogLevel.Information)
.AddConsole();
});
ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("Example log message");
}
}
Run Code Online (Sandbox Code Playgroud)
它在控制台上没有打印任何内容,为什么?
我安装了Rust 1.13并试过:
fn main() {
let x: u32;
x = 10; // no error?
}
Run Code Online (Sandbox Code Playgroud)
当我编译这个文件时有一些警告,但是没有错误.因为我不声明x作为mut,不应该x = 10;导致错误?
在 c++20 之前,this指针以 [=] 隐式捕获。那么,c++20 决定用户应该编写 [=, this] 来显式捕获此指针的原因是什么,我的意思是,如果没有这种更改,c++20 之前的代码可能会有任何代码味道或潜在的错误?
这种语言更改有什么好的示例或原因吗?
我有以下测试程序:
#include<memory>
#include<iostream>
using namespace std;
int main()
{
shared_ptr<int> si(new int(5));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
调试它:
(gdb) l
1 #include<memory>
2 #include<iostream>
3 using namespace std;
4
5 int main()
6 {
7 shared_ptr<int> si(new int(5));
8 return 0;
9 }
10
(gdb) b 8
Breakpoint 1 at 0x400bba: file testshare.cpp, line 8.
(gdb) r
Starting program: /home/x/cpp/x01/a.out
Breakpoint 1, main () at testshare.cpp:8
8 return 0;
(gdb) p si
$1 = std::shared_ptr (count 1, weak 0) 0x614c20
Run Code Online (Sandbox Code Playgroud)
它只打印出指针类型信息si …
我有 2 个实验环境:CentOS 6.8 和 Ubuntu 16.04 都在 Virtualbox VM 中运行。
在 CentOS 上我安装了
yum install kernel-devel kernel-debug
Run Code Online (Sandbox Code Playgroud)
在 Ubuntu 上我安装了:
sudo apt-get install linux-headers-$(uname -r)
sudo apt-get install fakeroot build-essential crash kexec-tools makedumpfile kernel-wedge
Run Code Online (Sandbox Code Playgroud)
在两个系统上我都可以成功运行:
stap -ve 'probe begin { log("hello world") exit() }'
Run Code Online (Sandbox Code Playgroud)
我.stp从systemtap指南中尝试了这个脚本:
#!/usr/bin/stap
probe begin
{
log("begin probe")
}
probe syscall.open
{
printf("%s(%d) open (%s)\n",execname(),pid(),argstr)
}
probe timer.ms(4000)#4s later
{
exit()
}
probe end
{
log("end probe")
}
Run Code Online (Sandbox Code Playgroud)
chmod +x ...脚本并以 …
c++ ×4
linux ×2
printing ×2
python ×2
this ×2
.net-core ×1
asp.net-core ×1
break ×1
c# ×1
c++20 ×1
capture ×1
compilation ×1
console ×1
const ×1
decltype ×1
exception ×1
gdb ×1
ilogger ×1
ipython ×1
iterator ×1
kernel ×1
lambda ×1
matrix ×1
mutable ×1
obsolete ×1
owin ×1
pointers ×1
reference ×1
rust ×1
shared-ptr ×1
syntax ×1
systemtap ×1
types ×1
variables ×1
windbg ×1
zip ×1