我想创建一个名为pos(从位置)的typedef结构来存储坐标x和y.我试图为这个结构重载一些运算符,但它不编译.
typedef struct {
int x;
int y;
inline pos operator=(pos a) {
x=a.x;
y=a.y;
return a;
}
inline pos operator+(pos a) {
return {a.x+x,a.y+y};
}
inline bool operator==(pos a) {
if (a.x==x && a.y== y)
return true;
else
return false;
}
} pos;
Run Code Online (Sandbox Code Playgroud)
我也想知道这个之间的区别:
inline bool operator==(pos a) {
if(a.x==x && a.y== y)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
bool operator==(pos a) const {
if(a.x==x && a.y== y)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud) 为什么赋值运算符在声明对象的同一行中完成时不允许使用lambda表达式?
它似乎在MSVC中工作.
测试代码:https: //godbolt.org/g/n2Tih1
class Func
{
typedef void(*func_type)();
func_type m_f;
public:
Func() {}
Func(func_type f) : m_f(f) {}
Func operator=(func_type f) {
m_f = f;
return *this;
}
};
int main()
{
// doesn't compile in GCC and clang, it does in MSVC
Func f1 = []() {
};
// compiles!
Func f2;
f2 = []() {
};
// compiles!
Func f3([]() {
});
}
Run Code Online (Sandbox Code Playgroud) 在SDL中,这两个窗口事件有什么区别?
SDL_WINDOWEVENT_RESIZED
SDL_WINDOWEVENT_SIZE_CHANGED
Run Code Online (Sandbox Code Playgroud)
在维基文档页面中有这个代码示例:
case SDL_WINDOWEVENT_RESIZED:
SDL_Log("Window %d resized to %dx%d",
event->window.windowID, event->window.data1,
event->window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
SDL_Log("Window %d size changed to %dx%d",
event->window.windowID, event->window.data1,
event->window.data2);
break;
Run Code Online (Sandbox Code Playgroud)
但我看不出有什么区别。
我正在尝试使用 pip 为 python 安装 OpenEXR 模块。
C:\Python27_64\Scripts\pip.exe install openexr
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Collecting openexr
Using cached OpenEXR-1.2.0.tar.gz
Installing collected packages: openexr
Running setup.py install for openexr ... error
Complete output from command c:\python27_64\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\tuket
\\appdata\\local\\temp\\pip-build-r6zwty\\openexr\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().r
eplace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\tuket\appdata\local\temp
\pip-lledgs-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-2.7
copying Imath.py -> build\lib.win-amd64-2.7
running build_ext
building 'OpenEXR' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Users\tuket\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox …Run Code Online (Sandbox Code Playgroud) 关闭和清理套接字的正确方法是什么?
我有 io_service 在辅助线程中运行,我需要关闭主线程的连接:
void closeConnection()
{
ioc.post([&socket]() {
// Which ones do I have to call?
// In what order?
// What do they do?
//socket.cancel();
//socket.shutdown(asio::ip::tcp::socket::shutdown_both);
//socket.close();
//socket.release();
});
secondaryThread.join();
}
Run Code Online (Sandbox Code Playgroud)
所有这些功能之间有什么区别?
我试过这个序列......
socket.cancel();
socket.close();
socket.release();
Run Code Online (Sandbox Code Playgroud)
并且似乎没有错误地关闭了连接但是花费了太多时间(大约 5-10 秒),所以我想我做错了什么。
我想这样做
#include <vector>
#include <span>
struct S
{
std::vector<int> v;
void set(std::span<int> _v)
{
v = _v;
}
};
Run Code Online (Sandbox Code Playgroud)
但它不编译。有哪些替代方案?
我制作了这个简单的 ELF 用于学习目的:
bits 64
org 0x08048000
elfHeader:
db 0x7F, "ELF", 2, 1, 1, 0 ; e_ident
db 0 ; abi version
times 7 db 0 ; unused padding
dw 2 ; e_type
dw 62 ; e_machine
dd 1 ; e_version
dq _start ; e_entry
dq programHeader - $$ ; e_phoff
dq 0 ; e_shoff
dd 0 ; e_flags
dw elfHeaderSize ; e_ehsize
dw programHeaderSize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw …Run Code Online (Sandbox Code Playgroud) 我试图声明一个函数,它接受与参数相同类型的函数.
void rec(void(*f)(void(*)(void(*)(...))))
{
f(f);
}
Run Code Online (Sandbox Code Playgroud)
我最终做了递归尝试.
你总是可以从一个演员void*.
void rec(void* f)
{
((void(*)())f)(f);
}
Run Code Online (Sandbox Code Playgroud)
但它不是类型安全的
我尝试用以下方法做到这一点typedef:
typedef void(*RecFunc)(RecFunc);
Run Code Online (Sandbox Code Playgroud)
但是没有编译.
有可能吗?
我正在实现一个特殊目的句柄类.
ihandle是一个所有句柄必须实现的接口,在我的实际代码中它将具有运算符重载-> *.但是对于这个例子,我想保持简单,它只是具有get功能.
template <typename T>
class ihandle {
public:
virtual T* get();
};
Run Code Online (Sandbox Code Playgroud)
一种可能的实现方式是ptr,它只是一个原始指针.
template <typename T>
class ptr : public ihandle<T>
{
T* t;
public:
ptr(T* t = nullptr) : t(t) {}
T* get(){return t;}
};
Run Code Online (Sandbox Code Playgroud)
然后有handle哪些用于进行空安全检查.
template <typename T>
class handle
{
public:
ihandle<T>* h;
T* get(){return h->get();}
handle(ihandle<T>* h = nullptr) : h(h) {}
template <typename D>
handle(handle<D>& hd)
: h((ihandle<T>*)hd.h)
{
static_assert(is_base_of<T, D>::value, "error");
}
}; …Run Code Online (Sandbox Code Playgroud) 编者注:
启用优化的后续问题仅对循环
计时:为什么通过`std :: vector`进行迭代比通过`std :: array`进行迭代更快?
在这里我们可以看到延迟分配页面错误在读取未初始化的BSS内存与在定时循环之外初始化的动态分配+写入内存的影响。
我尝试分析此代码:
#include <vector>
#include <array>
#include <stdio.h>
using namespace std;
constexpr int n = 400'000'000;
//vector<int> v(n);
array<int, n> v;
int main()
{
int res = 0;
for(int x : v)
res += x;
printf("%d\n", res);
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上,array版本比快vector。
在这种情况下,内存分配是无关紧要的,因为它只有一次。
$ g++ arrVsVec.cpp -O3
$ time ./a.out
0
real 0m0,445s
user 0m0,203s
sys 0m0,238s
Run Code Online (Sandbox Code Playgroud)
$ g++ arrVsVec.cpp -O3
$ time ./a.out
0
real 0m0,749s
user 0m0,273s
sys …Run Code Online (Sandbox Code Playgroud) 我最近问了一个问题: 为什么迭代std :: array比迭代std :: vector快得多?
正如人们很快指出的那样,我的基准测试存在许多缺陷。因此,当我尝试确定基准时,我注意到这std::vector并不慢std::array,实际上,情况恰恰相反。
#include <vector>
#include <array>
#include <stdio.h>
#include <chrono>
using namespace std;
constexpr int n = 100'000'000;
vector<int> v(n);
//array<int, n> v;
int main()
{
int res = 0;
auto start = chrono::steady_clock::now();
for(int x : v)
res += x;
auto end = chrono::steady_clock::now();
auto diff = end - start;
double elapsed =
std::chrono::duration_cast<
std::chrono::duration<double, std::milli>
>(end - start).count();
printf("result: %d\ntime: %f\n", res, elapsed);
}
Run Code Online (Sandbox Code Playgroud)
我尝试从以前的基准进行改进的事情:
-O3标志速度 …检查 zig 中是否存在文件的最简单方法是什么?
作为构建过程的一部分,我需要避免覆盖已经存在的文件。
我见过std.fs.access,但我不确定是否有更简单的方法。
这不编译:
vector<int[2]> v;
int p[2] = {1, 2};
v.push_back(p); //< compile error here
Run Code Online (Sandbox Code Playgroud)
什么是替代方案?我不想使用std::array.
std::vector自身的声明编译。这push_back是不编译的。