这是代码:
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
class Date
{
public:
Date(int year, int month, int day) : year(year), month(month), day(day) {}
Date(const Date &d) : year(d.year), month(d.month), day(d.day) {}
std::string to_string() {
std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << year << '-' << std::setw(2) << month << '-' << day;
return ss.str();
}
private:
int year, month, day;
};
int main()
{
std::vector<Date> vd;
vd.emplace_back(2017, 1, 13);
vd.emplace_back(vd[0]);
std::cout << vd.back().to_string() << "\n";
} …Run Code Online (Sandbox Code Playgroud) 考虑以下代码。如果我的理解if constexpr是正确的,则else不应编译分支,因此z()不应将其视为错误。
#include <type_traits>
struct Z{};
template<typename T>
void f(T z) {
auto lam = [z]() {
if constexpr(std::is_same<T, Z>::value) {
} else {
z();
}
};
}
int main() {
f(Z{});
}
Run Code Online (Sandbox Code Playgroud)
用clang和gcc编译;但是使用最新的MSVC却没有。不幸的是,goldbolt的MSVC太旧了,但是在我的计算机上使用VS 2017进行了全面更新,结果是cl /std:c++17:
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26428.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
if_constexpr.cpp
if_constexpr.cpp(10): error C2064: term does not evaluate to a function taking 0 arguments
if_constexpr.cpp(16): note: …Run Code Online (Sandbox Code Playgroud) 我试图了解Windows API如何创建进程,以便我可以创建一个程序来确定无效的exes失败的位置.我有一个打电话的程序kernel32.CreateProcessA.跟随OllyDbg,这个调用kernel32.CreateProcessInternalA调用kernel32.CreateProcessInternalW哪个调用ntdll.ZwCreateUserProcess.这个功能是:
mov eax, 0xAA
xor ecx, ecx
lea edx, dword ptr [esp+4]
call dword ptr fs:[0xC0]
add esp, 4
retn 0x2C
Run Code Online (Sandbox Code Playgroud)
所以我跟着调用fs:[0xC0],其中包含一条指令:
jmp far 0x33:0x74BE271E
Run Code Online (Sandbox Code Playgroud)
但是,当我踏上这个指令,奥利刚回来ntdll.ZwCreateUserProcess在add esp, 4通话之后(这是不是0x74BE271E).我给了一个断点retn 0x2C,我发现新进程是在执行期间以某种方式创建的add esp, 4.
所以我假设跳远有一些魔法.我试图将CS寄存器更改为0x33EIP 0x74BE271E而不是实际执行远跳,但这只是在几条指令后给了我一个访问冲突.这里发生了什么?我需要能够深入研究这个抽象,ZwCreateUserProcess以了解Windows究竟是如何创建流程的.
我有一个演员这样声明:
template<typename... Coords>
MyClass<T>(vector<T> values, Coords... coords) { /* code */ }
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:
template<typename... Coords>
MyClass<T>(Coords... coords, vector<T> values) { /* code */ }
Run Code Online (Sandbox Code Playgroud)
但标准要求可变参数是最后一个。如果我写类似的东西
template<typename... Args>
MyClass<T>(Args... coordsThenValues) { /* code */ }
Run Code Online (Sandbox Code Playgroud)
我将如何拆分coordsThenValues为最后一个参数包Coords... coords和最后一个参数vector<T> values?
我正在寻找一种数据结构,支持k从0到M-1的整数键的以下操作.
insert(k),erase(k),lookup(k).find_missing_key()返回结构中当前不存在的任何键的特殊操作.一个明显的实现是一个"list-of-free-keys"结构,实现为堆; 但这需要O(M)空间.是否有一些数据结构满足所有要求?
假设我有一个包含多个成员变量的类:
class MyClass{
std::string a;
int b;
SomeOtherClass c;
// some stuff...
public:
// some other stuff...
};
Run Code Online (Sandbox Code Playgroud)
我想定义operator<首先比较的关系运算符(等)a,但如果a相等,则比较b,但如果b相等,则比较c.(我们假设SomeOtherClass已经定义了关系运算符.)所以我有类似的东西
bool operator==(MyClass param){
return (a == param.a) && (b == param.b) && (c == param.c);
}
bool operator<(MyClass param){
if(a < param.a) return true;
if(a > param.a) return false;
if(b < param.b) return true;
if(b > param.b) return false;
if(c < param.c) return true;
return false;
} …Run Code Online (Sandbox Code Playgroud) 在MSVC 2012中:
const std::string tableString;
std::vector<size_t> trPosVec;
// other stuff...
std::for_each(trIterator, endIterator,
[&, tableString, trPosVec](const boost::match_results<std::string::const_iterator>& matches){
trPosVec.push_back(std::distance(tableString.begin(), matches[0].second));
}
);
Run Code Online (Sandbox Code Playgroud)
此代码提供工具提示错误:
Error: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=size_t, _Alloc=std::allocator<char32_t>]" matches the argument list and object (the object has type qualifiers that prevent a match)
argument types are: (ptrdiff_t)
object type is: const std::vector<size_t, std::allocator<char32_t>>
Run Code Online (Sandbox Code Playgroud)
我认为它意味着它是trPosVec按价值捕获的.当我明确指定捕获模式时,它工作正常[&tableString, &trPosVec].如果我尝试双重指定[&, tableString, &trPosVec],它会给出Error: explicit capture matches default.这里发生了什么?
我正在使用nats的元组(特别是三元组nat*nat*nat),并且希望有一种按字典顺序比较元组的方法。与此等效:
Inductive lt3 : nat*nat*nat -> nat*nat*nat -> Prop :=
| lt3_1 : forall n1 n2 n3 m1 m2 m3, n1 < m1 -> lt3 (n1,n2,n3) (m1,m2,m3)
| lt3_2 : forall n1 n2 n3 m2 m3, n2 < m2 -> lt3 (n1,n2,n3) (n1,m2,m3)
| lt3_3 : forall n1 n2 n3 m3, n3 < m3 -> lt3 (n1,n2,n3) (n1,n2,m3).
Run Code Online (Sandbox Code Playgroud)
我想证明一些基本特性,例如传递性和充分根据。标准库中是否有可以完成大部分工作的内容?如果没有,我对有根据的最感兴趣。我将如何证明呢?
在C中你可以做到int a[] = {1,2,3,4,5},但是C++ 11 std::array<int> a = {1,2,3,4,5}会给出"模板参数太少"的编译错误.有什么方法吗?