简短的例子:
#include <iostream>
int main()
{
int n;
[&](){n = 10;}(); // OK
[=]() mutable {n = 20;}(); // OK
// [=](){n = 10;}(); // Error: a by-value capture cannot be modified in a non-mutable lambda
std::cout << n << "\n"; // "10"
}
Run Code Online (Sandbox Code Playgroud)
问题:为什么我们需要mutable关键字?它与传统参数传递给命名函数有很大不同.背后的理由是什么?
我的印象是,按值捕获的整个点是允许用户更改临时值 - 否则我几乎总是更好地使用按引用捕获,不是吗?
有什么启示吗?
(顺便说一句,我使用的是MSVC2010.这应该是标准的AFAIK)
我正在研究一个守护进程,它通过inotify监视文件事件,以便在访问文件时触发各种类型的事件.我读过手表有点贵,因为内核存储了每个被监视文件的完整路径名.
有多少手表会太多?
编辑:大多数情况下,我想知道..你有没有看到一个明显的性能影响,如果是这样,它发生了多少手表?是的,我必须监控/递归(但它是一个最小的自举系统).
我发现这段代码不起作用:
typedef int (*fp)(int a, int b);
constexpr fp addition()
{
return [](int a, int b){ return a+b; };
}
#include <iostream>
int main()
{
fp fun = addition();
std::cout << fun(2,2);
}
Run Code Online (Sandbox Code Playgroud)
它给了我错误
cexpr.cpp: In function 'constexpr int (* addition())(int, int)':
cexpr.cpp:5:43: error: call to non-constexpr function 'addition()::<lambda(int,
int)>::operator int (*)(int, int)() const'
Run Code Online (Sandbox Code Playgroud)
这是为什么?我不是在这里打电话.
直接进场工作:
typedef int (*fp)(int a, int b);
#include <iostream>
int main()
{
fp fun = [](int a, int b){ return a+b; };
std::cout << fun(2,2); …Run Code Online (Sandbox Code Playgroud) 在阅读http://en.cppreference.com/w/cpp/algorithm/binary_search时,我注意到它将迭代器作为参数.现在我很困惑,因为我认为它宁愿是一个随机访问迭代器,所以二进制搜索实际上是二进制的.
为了满足我的好奇心,我写了一个小程序:
#include <iostream>
#include <vector>
#include <forward_list>
#include <list>
#include <deque>
#include <algorithm>
#include <chrono>
#include <random>
int main()
{
std::uniform_int_distribution<int> uintdistr(-4000000, 4000000);
std::mt19937 twister(std::chrono::high_resolution_clock::to_time_t(std::chrono::high_resolution_clock::now()));
size_t arr[] = { 200000, 400000, 800000, 1600000, 3200000, 6400000, 12800000 };
for(auto size : arr)
{
std::list<int> my_list;
for(size_t i = 0; i < size; i++)
my_list.push_front(uintdistr(twister));
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
my_list.sort(); //fixed
start = std::chrono::high_resolution_clock::now();
std::binary_search(my_list.begin(), my_list.end(), 1252525);
end = std::chrono::high_resolution_clock::now();
long long unsigned elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::cout << "Test finished …Run Code Online (Sandbox Code Playgroud) 按照N4562提案,新提出的std::shared_ptr::operator[]发生在std::ptrdiff_t,这是一个符号类型.
这与标准库中的每个索引运算符不一致.甚至std::unique_ptr::operator[]需要std::size_t.
这个决定的理由是什么?
如果我使用std :: CIN,性病::法院和std ::字符串,是否有任何可能有人会利用缓冲区溢出?
我问这个是因为我仍然看到很多人在C++中仍然使用以null结尾的字符串而不是标准容器.
我正在写一个跳过列表.
是)我有的:
template<typename T>
struct SkipListNode
{
T data;
SkipListNode* next[32];
};
Run Code Online (Sandbox Code Playgroud)
这段代码的问题在于浪费空间 - 它要求所有节点都包含32个指针.特别是考虑到在典型列表中,一半节点只需要一个指针.
C语言有一个称为灵活数组成员的简洁功能,可以解决这个问题.如果它存在于C++中(即使对于普通的类),我可以编写如下代码:
template<typename T>
struct SkipListNode
{
alignas(T) char buffer[sizeof(T)];
SkipListNode* next[];
};
Run Code Online (Sandbox Code Playgroud)
然后手动创建具有工厂函数的节点,并在删除元素时销毁它们.
这带来了一个问题 - 如何在C++中没有未定义的行为的情况下可移植地模拟这样的功能?
我考虑malloc了缓冲区然后手动操作偏移 - 但是很容易违反对齐要求 - 如果你malloc(sizeof(char) + sizeof(void*)*5),指针是不对齐的.另外,我甚至不确定这些手工创建的缓冲区是否可以移植到C++.
请注意,我不需要确切的语法,甚至不需要使用 - 这是一个节点类,在跳过列表类的内部,根本不是接口的一部分.
std::functionfunc.wrap.func中的概要告诉我们
function& operator=(function&&);
移动赋值运算符不是noexcept,禁止使用仅移动标准容器中的类型.
但!它也告诉我们
void swap(function&) noexcept;
同样,默认构造函数是
function() noexcept;
因此,我们可以使用默认构造函数后跟交换来实现移动构造函数.
因为我们可以实现移动赋值运算符swap(swap具有更强的后置条件):
noexcept实现互换std::function?std::function移动赋值运算符不是noexcept?我正在阅读一些关于STL的文档,并且在那里写了该end()函数返回容器的最后一个元素旁边的字节的迭代器.
我想知道,如果容器占用整个可用内存的最后一个字节,该怎么办?那会发生什么?
考虑
class X
{
public:
std::unique_ptr<int> m_sp;
A m_a;
A test1()
{
return std::move(m_a);
}
A&& test2()
{
return std::move(m_a);
}
std::unique_ptr<int> test3()
{
return std::move(m_sp);
}
std::unique_ptr<int>&& test4()
{
return std::move(m_sp);
}
std::unique_ptr<int> test5()
{
return std::make_unique<int>(50);
}
};
class A
{
public:
A()
{
m_i = 1;
}
A(A&& other)
{
this->m_i = other.m_i;
other.m_i = -1;
}
A& operator=(A&& other)
{
this->m_i = other.m_i;
other.m_i = -1;
return *this;
}
int m_i;
};
Run Code Online (Sandbox Code Playgroud)
练习这些课程
X x;
A …Run Code Online (Sandbox Code Playgroud)