The C++ Standard Library (Second Edition)Nicolai Josuttis 在其着作中指出,编译器可以比普通函数更好地优化lambdas.
此外,C++编译器比普通函数更好地优化lambdas.(第213页)
这是为什么?
我认为在内联时不应该有任何差别.我能想到的唯一原因是编译器可能有一个更好的本地上下文与lambdas,这样可以做出更多假设并执行更多优化.
如果我有两个向量并想将它们组合成一个,我可以通过以下方式实现:
std::vector<T> a(100); // just some random size here
std::vector<T> b(100);
a.insert(std::end(a), std::begin(b), std::end(b));
Run Code Online (Sandbox Code Playgroud)
这涉及复制,但我想避免.有没有办法使用move-semantics将它们组合在一起?
我非常怀疑它,因为vector它应该是连续的.但是有什么方法可以做到deque吗?
不久前我写了一个程序,它使用了一些阶乘函数.我使用long double数据类型来支持"相对"大数字.
现在,我从代码块改为Visualstudio 2010,我想知道为什么我的程序不再工作,直到我在一些研究中意识到MS已经放弃了长双数据类型.这有什么特别的原因吗?对我而言,它看起来非常像技术方面的倒退.
有没有其他选择?(我也很乐意使用boost库中的替代品).
如何绑定到接受默认参数的函数,而不指定默认参数,然后在没有任何参数的情况下调用它?
void foo(int a, int b = 23) {
std::cout << a << " " << b << std::endl;
}
int main() {
auto f = std::bind(foo, 23, 34); // works
f();
auto g = std::bind(foo, 23); // doesn't work
g();
using std::placeholders::_1;
auto h = std::bind(foo, 23, _1); // doesn't work either
h();
}
Run Code Online (Sandbox Code Playgroud) 在基于范围的for循环中声明循环变量是否合法,我在循环的表达式语句中使用了相同的名称?我希望这个例子能说清楚.
#include <iostream>
#include <vector>
struct bar {
std::vector<int> nums;
};
int main()
{
bar b;
b.nums = {1, 2, 3};
for(int b : b.nums)
std::cout << b << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当clang 3.2允许时,gcc 4.8给出错误.
我想和Boost.Filesystem一起使用-fno-exceptions.根据Boost.Filesystem文档,它声明它支持BOOST_NO_EXCEPTIONS宏.
但是,以下代码段:
#define BOOST_NO_EXCEPTIONS
#include <boost/filesystem.hpp>
int main() {}
Run Code Online (Sandbox Code Playgroud)
编译:
g ++ -fno-exceptions boost_test.cpp
给出错误:
/.../boost/filesystem/operations.hpp:在构造函数'boost :: filesystem :: filesystem_error :: filesystem_error(const string&,boost :: system :: error_code)':/.../boost/filesystem/operations .hpp:84:16:错误:禁用异常处理,使用-fexceptions启用catch(...){m_imp_ptr.reset(); }
我在Mac OSX上使用gcc 5和boost 1.57进行编译(也在类似的ubuntu设置上进行了测试).
我想知道我的理解BOOST_NO_EXCEPTIONS是否正确,因为它应该涵盖使用-fno-exceptions或是否只是在那boost::throw_exception部分?
考虑我有lamba foo只做一些东西,不需要返回任何东西.当我这样做:
std::future<T> handle = std::async(std::launch::async, foo, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
一切都运行良好,lamba将在一个新的线程中产生.然而,当我不存储std::future其std::async返回时,foo将在主线程中运行,并阻止它.
std::async(std::launch::async, foo, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
阅读boost :: asio的文档,我还不清楚何时需要使用asio :: strand.假设我有一个使用io_service的线程,那么可以安全地在套接字上写入如下吗?
void Connection::write(boost::shared_ptr<string> msg)
{
_io_service.post(boost::bind(&Connection::_do_write,this,msg));
}
void Connection::_do_write(boost::shared_ptr<string> msg)
{
if(_write_in_progress)
{
_msg_queue.push_back(msg);
}
else
{
_write_in_progress=true;
boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
boost::bind(&Connection::_handle_write,this,
boost::asio::placeholders::error));
}
}
void Connection::_handle_write(boost::system::error_code const &error)
{
if(!error)
{
if(!_msg_queue.empty())
{
boost::shared_ptr<string> msg=_msg_queue.front();
_msg_queue.pop_front();
boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
boost::bind(&Connection::_handle_write,this,
boost::asio::placeholders::error));
}
else
{
_write_in_progress=false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
多个线程调用Connection :: write(..)或者我必须使用asio :: strand吗?
我想知道为什么我不能在C++ 14(或17)中做到这一点
std::shared_timed_mutex mutex;
std::unique_lock<std::shared_timed_mutex> lock(mutex);
std::condition_variable var;
while(!some_condition)
var.wait(lock);
Run Code Online (Sandbox Code Playgroud)
条件变量似乎只适用于std :: mutex.但为什么?
c++ ×10
c++11 ×4
boost ×2
asynchronous ×1
bind ×1
boost-asio ×1
c++14 ×1
c++17 ×1
deque ×1
exception ×1
for-loop ×1
lambda ×1
locking ×1
long-double ×1
move ×1
optimization ×1
scope ×1
vector ×1