boost :: bind在很多情况下非常方便.其中之一是调度/发布一个方法调用,以便io_service稍后可以进行调用.
在这种情况下,boost :: bind的行为就像人们可能坦率地期望的那样:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
boost::asio::io_service ioService;
class A {
public: A() {
// ioService will call B, which is private, how?
ioService.post(boost::bind(&A::B, this));
}
private: void B() {}
};
void main()
{
A::A();
boost::asio::io_service::work work(ioService);
ioService.run();
}
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,boost创建了一个functor(一个带有operator()()的类,能够在给定对象上调用给定的方法.该班级是否可以访问私人B?我猜不会.
我在这里错过了什么?
请考虑以下代码:
class A
{
public:
void foo()
{
auto functor = [this]()
{
A * a = this;
auto functor = [a]() // The compiler won't accept "this" instead of "a"
{
a->bar();
};
};
}
void bar() {}
};
Run Code Online (Sandbox Code Playgroud)
在VC2010中,使用this
而不是a
导致编译错误.其中:
1>main.cpp(20): error C3480: '`anonymous-namespace'::<lambda0>::__this': a lambda capture variable must be from an enclosing function scope
1>main.cpp(22): error C3493: 'this' cannot be implicitly captured because no default capture mode has been specified
Run Code Online (Sandbox Code Playgroud)
哪个我不明白.这是否意味着它不知道它是应该使用引用还是复制它?在尝试使用&this
强制引用时,它还说:
1>main.cpp(20): …
Run Code Online (Sandbox Code Playgroud) 通过引用传递对象是将地址传递给它的更简单,更快速和更安全的方式.但是对于大多数编译器来说,它们完全相同:引用确实是指针.
现在基本类型int
怎么样?将地址传递int
给函数并在函数内部使用它比复制传递要慢,因为在使用之前需要取消引用指针.
现代编译器如何处理,这个?
int foo(const int & i)
{
cout << i; // Do whatever read-only with i.
}
Run Code Online (Sandbox Code Playgroud)
我可以相信他们将此编译成这个吗?
int foo(const int i)
{
cout << i;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,在某些情况下,甚至可能会更快地同时通过i
和&i
,然后用i
阅读和*i
写作.
int foo(const int i, int * ptr_i)
{
cout << i; // no dereferencement, therefore faster (?)
// many more read-only operations with i.
*ptr_i = 123;
}
Run Code Online (Sandbox Code Playgroud) 我有一个与光线追踪相关的宠物项目.我需要生成光线跟踪图像,但实际的光线跟踪不是这里的重点.因此,我希望能够自由地使用库来为我做这件事.
我知道POVray但有点害怕它有两个原因:
我已经谷歌搜索了几天,除了POV之外没有找到任何合适的东西.
问题是...
我是否应该克服它并使用POVray,或者是否有简单(但不简单),更小,隐藏的光线跟踪库可以免费提供,如在言论自由中?
I created SubCtrl inheriting UserControl. It has no code. I created then Ctrl, which also inherits UserControl. It has a SubCtrl in it and its only code means to expose it publicly so it appears in the property list of Ctrl:
public subctrl.SubCtrl SUBCTRL
{
get { return this.subCtrl1; }
}
Run Code Online (Sandbox Code Playgroud)
Then I created a simple Form project which only has a Ctrl in it and no code. As I wanted, SUBCTRL appears in the property list of Ctrl so …
可变参数模板可以将某些类型的函数重写为更清晰,类型安全的版本.它的情况下printf
,作为例子给出维基百科:
void printf(const char *s)
{
while (*s) {
if (*s == '%' && *(++s) != '%')
throw std::runtime_error("invalid format string: missing arguments");
std::cout << *s++;
}
}
template<typename T, typename... Args>
void printf(const char *s, T value, Args... args)
{
while (*s) {
if (*s == '%' && *(++s) != '%') {
std::cout << value;
++s;
printf(s, args...); // call even when *s == 0 to detect extra arguments
return;
}
std::cout << *s++;
} …
Run Code Online (Sandbox Code Playgroud) 我想在 Mantis Bug Tracker 的路线图中对即将推出的软件版本进行估算。
我可以很容易地弄清楚如何向问题添加自定义字段(整数类型,称为“完成天数”),以及如何在“查看问题”页面上显示它。但我不知道如何在路线图中添加一些内容,显示未解决问题的完成天数。
有没有内置的方法来做到这一点?我的猜测是否定的,这需要在 Mantis 中进行一些 php 编码。但也许有人已经这样做了?
编辑:我查看了时间跟踪功能。看起来不像我想要的。
实现这一点应该是微不足道的
template<typename T>
T & as_is(T & t) { return t; }
Run Code Online (Sandbox Code Playgroud)
不过,我还是不想写它(:
我在www.cplusplus.com上找不到这样的东西.
对于那些会问"你想做什么"的人来说,就是这样.我有一个构建ascii表的类,有很好的填充和一切.我会遗漏细节.重要的是它存储字符串(因此它能够计算填充多少).我想实现一个sort函数,并能告诉该类将该列用作某种类型.如果我想按一列整数排序(这也是内部字符串),我会通过atoi
.如果排序字符串,我想传递as_is
,或者stl等价物,如果有的话.
据我所知,deque背后的动机是提供一个有效的随机访问容器push_front
.
与deque相比,vector的常见优点包括更快的遍历at()
,但主要是它的C兼容性,因为它保证了连续的内存.Deque没有,因为它是一大堆内存,每个都包含许多值.
我糊涂了.0
除了索引之后保留的内存之外,为什么deque不是像向量一样构建,而是在索引之前保留内存size-1
?这将保证连续内存,启用高效push_front
,甚至在解除引用迭代器时避免额外的间接.
为了最小化插入期间的移位,要移位的包含值将取决于插入点.如果在索引n
之前插入,则将size()/2
值n
向左移动.否则后移右值n
.
我错过了什么是非常重要的,deque是一组价值数组而不是一个大数组?
据我所知,unordered_
stl 容器保留了许多桶,这些桶的数量根据容器中元素的数量而变化。插入时,如果突破一定限制,容器将重新散列以使用更多存储桶,因此每个存储桶不会那么满,搜索速度也会更快。这会使迭代器无效。
这意味着我不应该将迭代器保留在unordered
容器中。除非我可以,如果我在重新整理后更新它们。但我找不到可靠的方法来检查是否insert
(无论是它emplace
还是其他什么)导致了重新哈希。
我应该监控吗bucket_count()
?
cppreference说Rehashing occurs only if the new number of elements is greater than max_load_factor()*bucket_count()
。这有保证吗?执行以下操作可靠吗?
bool will_rehash = (max_load_factor()*bucket_count()) > size()+1;
Run Code Online (Sandbox Code Playgroud)