请考虑以下最小示例:
#include <iostream>
using namespace std;
class myostream : public ostream {
public:
myostream(ostream const &other) :
ostream(other.rdbuf())
{ }
};
int main() {
cout << "hello world" << endl;
myostream s(cout);
s << "hello world" << endl;
myostream(cout) << "hello world" << endl;
}
Run Code Online (Sandbox Code Playgroud)
g ++和Visual C++上的输出都是
hello world
hello world
0x4012a4
Run Code Online (Sandbox Code Playgroud)
写入临时对象的版本myostream(cout)似乎更喜欢成员运算符ostream::operator<<(void *),而不是自由运算符operator<<(ostream &, char *).对象是否有名称似乎有所不同.
为什么会这样?我该如何防止这种行为?
编辑:为什么它发生现在从各种答案清楚.至于如何防止这种情况,以下似乎很吸引人:
class myostream : public ostream {
public:
// ...
myostream &operator<<(char const *str) …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading member-functions overload-resolution
如何使用asp.net&c#.net中的文件上传控件过滤文件类型
例如,单击文件上传控件的浏览按钮时,应该只打开excel文件类型的浏览文件对话框.
这怎么可能
我正在尝试清理和XSS证明来自客户端的一些HTML输入.我正在使用Python 2.6和Beautiful Soup.我解析输入,剥离不在白名单中的所有标签和属性,并将树转换回字符串.
然而...
>>> unicode(BeautifulSoup('text < text'))
u'text < text'
Run Code Online (Sandbox Code Playgroud)
对我来说,这看起来不像是有效的HTML.使用我的标签剥离器,它打开了各种各样的肮脏的方式:
>>> print BeautifulSoup('<<script></script>script>alert("xss")<<script></script>script>').prettify()
<
<script>
</script>
script>alert("xss")<
<script>
</script>
script>
Run Code Online (Sandbox Code Playgroud)
这些<script></script>对将被删除,剩下的不仅是XSS攻击,甚至是有效的HTML.
The obvious solution is to replace all < characters by < that, after parsing, are found not to belong to a tag (and similar for >&'"). But the Beautiful Soup documentation only mentions the parsing of entities, not the producing of them. Of course I can run a replace over all NavigableString nodes, but …
这个问题主要是学术问题.我出于好奇而不是因为这给我带来了实际问题.
考虑以下不正确的C程序.
#include <signal.h>
#include <stdio.h>
static int running = 1;
void handler(int u) {
running = 0;
}
int main() {
signal(SIGTERM, handler);
while (running)
;
printf("Bye!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此程序不正确,因为处理程序中断了程序流,因此running可以随时修改,因此应该声明volatile.但是,让我们说程序员忘记了这一点.
gcc 4.3.3,带有-O3标志,将循环体(在对running标志进行一次初始检查后)编译为无限循环
.L7:
jmp .L7
Run Code Online (Sandbox Code Playgroud)
这是可以预料的.
现在我们在while循环中放入一些微不足道的东西,比如:
while (running)
putchar('.');
Run Code Online (Sandbox Code Playgroud)
突然间,gcc不再优化循环条件了!循环体的组件现在看起来像这样(再次-O3):
.L7:
movq stdout(%rip), %rsi
movl $46, %edi
call _IO_putc
movl running(%rip), %eax
testl %eax, %eax
jne .L7
Run Code Online (Sandbox Code Playgroud)
我们看到running每次循环都会从内存中重新加载; 它甚至没有缓存在寄存器中.显然gcc现在认为价值running可能会发生变化.
那么为什么gcc突然决定需要重新检查running …
我有一个无向的、正加权的、连接的图,带有 verticesV和边E。我也有一个S顶点子集。现在V包含大约 22000 个顶点和E大约 23000 个边,但是对于更大的输入,这些预计会增加到大约 100 万个。S另一方面,通常包含少于 1000 个顶点,并且它们在图中相对靠近。
我想找到 的“中心” S,意思是一个顶点c,V从它到最远顶点的距离S尽可能小。它就像图中心,但仅适用于顶点的子集。[编辑:] 这也是图形上的1 中心问题;更一般的k 中心问题是 NP-hard 问题,但这个问题可能更容易。
有没有一种算法可以有效地找到这个中心?理想情况下,性能仅取决于S及其周围环境,而不取决于整个图。
我想过开始从所有顶点的广度优先搜索s_i的S同时,当一个顶点停止v_i已经被所有遇到的s_i,但是这是不是太有效。在这种情况下它可能是可行的,但感觉可能有更好的方法。
我正在实现一个继承自的自定义控件Control.我希望它是可聚焦的(它是一种列表框).
在构造函数中,我这样做
SetStyle(ControlStyles.Selectable, true);
Run Code Online (Sandbox Code Playgroud)
我现在可以使用Tab导航到控件.
但是,当控件收到鼠标单击时,它不会自动声明焦点.当然,我可以解决这个问题:
protected override void OnMouseDown(MouseEventArgs e)
{
Focus();
base.OnMouseDown(e);
}
Run Code Online (Sandbox Code Playgroud)
但这感觉就像一个不应该是必需的kludge.这真的是要走的路吗?或者有什么方法可以告诉Control您在收到鼠标点击时自动声明焦点?
我正在尝试使用预加载脚本来解决Electron 4.2.3 中的CORS 标头问题。但是,我无法运行预加载脚本。一个最小的复制案例:
包.json
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"dependencies": {
"electron": "^4.2.3"
}
}
Run Code Online (Sandbox Code Playgroud)
主文件
const { app, BrowserWindow } = require('electron')
app.on('ready', function() {
const win = new BrowserWindow({
webPreferences: {
preload: `file://${__dirname}/preload.js`,
}
})
win.webContents.openDevTools()
win.loadFile('index.html')
})
Run Code Online (Sandbox Code Playgroud)
预加载.js
window.preloadWasRun = 'preload was run'
Run Code Online (Sandbox Code Playgroud)
索引.html
<body>
<script>
document.write(window.preloadWasRun || 'preload was not run')
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
无论我对webSecurity,nodeIntegration和使用什么设置contextIsolation,似乎我的preload脚本都被忽略了。即使我在脚本中犯了语法错误,它也不会在任何地方显示任何错误。
我正在开发一个非常标准的 Flutter 项目。因为我是 Flutter 和 Dart 的新手,所以我希望我的工具尽可能有用。所以我添加pedantic: ^1.9.0并这样dev_dependencies写:analysis_options.yaml
include: package:pedantic/analysis_options.yaml\n\nanalyzer:\n exclude: [build/**]\n strong-mode:\n implicit-casts: false\n implicit-dynamic: false\nRun Code Online (Sandbox Code Playgroud)\n\n根据https://dart-lang.github.io/linter/lints/,pedantic应该启用avoid_empty_else和avoid_relative_lib_importslints 等错误。但是当我编写如下代码时:
import \'../model/model.dart\';\nRun Code Online (Sandbox Code Playgroud)\n\n或这个:
\n\n if (context == null) {\n print(\'context is null\');\n } else {\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我在 IntelliJ IDEA 中没有收到任何错误,flutter analyze手动运行时也没有收到任何错误:
$ flutter analyze\nAnalyzing app... \nNo issues found! (ran in 5.0s)\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试显式启用这些 lints:
\n\n$ flutter analyze\nAnalyzing app... \nNo …Run Code Online (Sandbox Code Playgroud) 我正在设置一个低级鼠标钩子SetWindowsHookEx:
HANDLE handle = SetWindowsHookEx(WH_MOUSE_LL,
&callback,
GetModuleHandle(NULL),
NULL);
Run Code Online (Sandbox Code Playgroud)
因为这是一个低级回调,它将在我自己的进程中执行; 没有执行DLL注入.
现在,我注意到回调有时(间接地)从标准API函数调用,例如GetAncestor,GetWindowRect等等.看起来这些可能会导致某些消息队列被刷新.
实际上,我的问题有三个......
什么叫回调?
可以从任何 API函数内部调用吗?我怎么说?
在什么线程上执行回调?
它只能在安装了挂钩的线程上运行,还是系统可以在任何线程上调用它?
为什么钩子首先被实现为回调?
(Raymond Chen在这里闲逛吗?)对于我来说,将钩子简单地实现为(发送)消息似乎更为明智,就像几乎所有其他Windows一样.对于消息,至少我知道哪个功能可能会导致待处理发送的消息将被处理(GetMessage,PeekMessage以及其他的少数),我会知道哪个线程它们被处理(即在第一时间接收到的消息的线程).
我正在尝试编写一个Syslog监听器,到目前为止,它很好地通过TCP接受传入消息,但我也希望UDP能够运行.
这是我正在使用的UDP服务器代码,它使用python客户端应用程序.我还有另一个应用程序也可以使用python客户端应用程序.
# Server program
# UDP VERSION
from socket import *
# Set the socket parameters
host = "localhost"
port = 514
buf = 1024
addr = (host,port)
# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"
# Close socket
UDPSock.close()
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以发送到服务器并让它显示代码.
# Client program
from socket import *
# Set the socket parameters
host …Run Code Online (Sandbox Code Playgroud)