请考虑以下代码:
int a = 1;
/* Note that const is on the right here */
int & const b = a;
Run Code Online (Sandbox Code Playgroud)
当然,它没有任何意义(因为引用就像非空常量指针),但是这个代码在MSVC 2015和MSVC 2017中编译得很好.GCC和Clang无法按预期编译它:
错误:'const'限定符不能应用于'int&'
这是有意的吗?这是一个错误吗?
int a = 1;
int const const const & const const const const b = a;
Run Code Online (Sandbox Code Playgroud)
这段代码编译也很好.
考虑下面这段代码:
import timeit
import dis
class Bob(object):
__slots__ = "_a",
def __init__(self):
self._a = "a"
@property
def a_prop(self):
return self._a
bob = Bob()
def return_attribute():
return bob._a
def return_property():
return bob.a_prop
print(dis.dis(return_attribute))
print(dis.dis(return_property))
print("attribute:")
print(timeit.timeit("return_attribute()",
setup="from __main__ import return_attribute", number=1000000))
print("@property:")
print(timeit.timeit("return_property()",
setup="from __main__ import return_property", number=1000000))
Run Code Online (Sandbox Code Playgroud)
很容易看到return_attribute并return_property导致相同的字节码:
17 0 LOAD_GLOBAL 0 (bob)
3 LOAD_ATTR 1 (_a)
6 RETURN_VALUE
None
20 0 LOAD_GLOBAL 0 (bob)
3 LOAD_ATTR 1 (a_prop)
6 RETURN_VALUE
None
Run Code Online (Sandbox Code Playgroud)
但是,时间有所不同:
17 0 LOAD_GLOBAL …Run Code Online (Sandbox Code Playgroud) 我正在尝试将printf输出转换为std::string变量。我有以下for循环:
for(i=0; i<53; i++) {
printf("%02X", pbRecvBuffer[i]);
}
Run Code Online (Sandbox Code Playgroud)
此循环的输出为十六进制值,例如:01445420434F2..。我的第一次尝试是使用stringstream以下方法:
stringstream os;
for(i=0; i<53; i++)
os << std::hex << pbRecvBuffer[i];
std::cout << os << std::endl;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我错误的结果。也许有人直接看到它可以帮助我。
假设我有以下代码:
// exposition-only, I can't use this macro in my solution
#if defined(HAS_MY_ENUM)
enum my_enum {
zero,
one,
};
#endif
enum empty {
placeholder, // an enum must always have a value
};
Run Code Online (Sandbox Code Playgroud)
我希望有一个条件类型别名my_enum_or_empty,如果已定义,则设置为my_enum,否则设置为empty,例如:
using my_enum_or_empty = std::conditional<my_enum_exists, my_enum, empty>;
Run Code Online (Sandbox Code Playgroud)
我想要一个基于 SFINAE 的解决方案,它可以为我提供my_enum_exists.
我正在考虑这种方法,但它需要 的前向声明my_enum,并且不能前向声明,因为它是无大小的。我无法添加尺寸,因为它enum来自外部库。你有什么想法?
我的用例是,这my_enum是在我无法控制的外部 C 库中定义的。我需要针对该库的两个版本(带my_enum和不带)编译我的代码。定义的标头源my_enum不能更改,即它必须是旧的 C 样式enum。
编译时不知道库版本。可惜没有LIBRARY_VERSION宏。我只需要依靠这一点 …
我正在尝试在 Linux 上用 C++挂钩recv()和send()函数。
我知道如何挂钩函数 (github: zeek/subhook)。但是我想要一些帮助来学习如何查找recv()或send()函数的地址(在运行时,或使用独立于版本的解决方案)。
我愿意接受任何可以帮助我理解此处涉及的机制的文档或建议。
[编辑] 澄清:我不想使用,LD_PRELOAD=因为我用这个工具注入了我的共享库:linux-inject。
我有一个 python 脚本,可以生成在后续编译中使用的.cpp源。该脚本使用一些第三方模块,我正在尝试设置PYTHONPATH模块位置。
我尝试这样做set(ENV${PYTHONPATH} "/path/to/modules"),但不幸的是它仅适用于生成时间execute_process(COMMAND python the_script.py),即,而我需要它工作add_custom_command/add_custom_target。
我正在使用Visual Studio 14 2015 Win64生成器。
我使用以下代码下载并绘制 AAPL 每日股票价格:
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
ts = TimeSeries(key='YOUR_API_KEY', output_format='pandas')
data, meta_data = ts.get_daily(symbol='AAPL', outputsize='full')
plt.figure(figsize=(10,6))
data['3. low'].plot()
plt.grid(linestyle='-', linewidth=2)
plt.title('AAPL stock price daily')
plt.savefig('sample.png')
plt.show()
Run Code Online (Sandbox Code Playgroud)
显然,2015年的这次降价看起来不太对劲。而且 AAPL 从来没有这么贵过。此外,该数据与其他股票价格来源(例如谷歌)相矛盾。
我是否滥用 API?这是一个错误吗?
假设我想合并branch_B到branch_A. 我做了:
git checkout branch_A
git merge branch_B
Run Code Online (Sandbox Code Playgroud)
这导致了冲突之墙,我花了很长时间才解决。然后,我必须紧急做一些事情branch_C。我做了:
git stash
git checkout branch_C
Run Code Online (Sandbox Code Playgroud)
现在,我已经完成branch_C并想继续我的合并。我做了:
git checkout branch_A
git stash pop
Run Code Online (Sandbox Code Playgroud)
然而,合并现在被视为branch_A与我的合并尝试之间的简单差异。当合并正在进行时,如何取消隐藏这些更改?
考虑这个 memcpy 类似的功能:
void copy(unsigned *restrict const dst, unsigned const *restrict const src, unsigned long n)
{
for (unsigned long x = 0; x < n; ++x)
{
dst[x] = src[x];
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码很好地优化到了 memcpy:
copy:
cbz x2, .L1
lsl x2, x2, 2
b memcpy
.L1:
ret
Run Code Online (Sandbox Code Playgroud)
但是,当我删除时restrict,会clang应用循环矢量化并且不会将其替换为memmove. 这是为什么?
我尝试在启用优化报告的情况下编译它:
clang-10 main.c -c -O3 -fsave-optimization-record -S && cat ./main.opt.yaml
Run Code Online (Sandbox Code Playgroud)
这就是我得到的restrict:
--- !Passed
Pass: loop-idiom
Name: ProcessLoopStoreOfLoopLoad
DebugLoc: …Run Code Online (Sandbox Code Playgroud) 我想编写一个启动另一个应用程序的应用程序.像这样:
# This will launch another_app.exe
my_app.exe another_app.exe
# This will launch another_app.exe with arg1, arg and arg3 arguments
my_app.exe another_app.exe arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
这里的问题是我正在进入char* argv[]我的main函数,但我需要将它合并到LPTSTR以便将其传递给CreateProcess.
有一个GetCommandLine函数,但我不能使用它,因为我从Linux移植代码并绑定argc/argv(否则,这对我来说是一个非常难看的黑客).
我不能轻易地手动合并参数,因为argv[i]可能包含空格.
基本上,我想反过来CommandLineToArgvW.有没有标准的方法来做到这一点?