小编inf*_*inf的帖子

为什么lambdas可以通过编译器比普通函数更好地优化?

The C++ Standard Library (Second Edition)Nicolai Josuttis 在其着作中指出,编译器可以比普通函数更好地优化lambdas.

此外,C++编译器比普通函数更好地优化lambdas.(第213页)

这是为什么?

我认为在内联时不应该有任何差别.我能想到的唯一原因是编译器可能有一个更好的本地上下文与lambdas,这样可以做出更多假设并执行更多优化.

c++ optimization lambda compiler-optimization c++11

167
推荐指数
2
解决办法
2万
查看次数

std :: lock_guard或std :: scoped_lock?

C++ 17引入了一个名为的新锁类std::scoped_lock.

从文档来看,它看起来与现有的std::lock_guard类相似.

有什么区别,什么时候应该使用它?

c++ multithreading locking c++17

106
推荐指数
4
解决办法
2万
查看次数

将两个向量"移动"在一起

如果我有两个向量并想将它们组合成一个,我可以通过以下方式实现:

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吗?

c++ move vector deque c++11

25
推荐指数
2
解决办法
5966
查看次数

为什么微软放弃了长双数据类型?

不久前我写了一个程序,它使用了一些阶乘函数.我使用long double数据类型来支持"相对"大数字.

现在,我从代码块改为Visualstudio 2010,我想知道为什么我的程序不再工作,直到我在一些研究中意识到MS已经放弃了长双数据类型.这有什么特别的原因吗?对我而言,它看起来非常像技术方面的倒退.

有没有其他选择?(我也很乐意使用boost库中的替代品).

c++ floating-point long-double visual-studio

21
推荐指数
1
解决办法
7695
查看次数

我可以绑定到一个接受默认参数然后调用它的函数吗?

如何绑定到接受默认参数的函数,而不指定默认参数,然后在没有任何参数的情况下调用它?

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)

c++ bind default-arguments c++11

21
推荐指数
2
解决办法
7266
查看次数

在基于范围的表达式和声明中具有相同名称的标识符

在基于范围的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给出错误.

c++ scope for-loop c++11

16
推荐指数
2
解决办法
317
查看次数

BOOST_NO_EXCEPTIONS是否保证与-fno-exceptions的兼容性?

我想和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部分?

c++ boost exception

16
推荐指数
1
解决办法
1591
查看次数

当未存储返回值时,std :: async不会生成新线程

考虑我有lamba foo只做一些东西,不需要返回任何东西.当我这样做:

std::future<T> handle = std::async(std::launch::async, foo, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)

一切都运行良好,lamba将在一个新的线程中产生.然而,当我不存储std::futurestd::async返回时,foo将在主线程中运行,并阻止它.

std::async(std::launch::async, foo, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

c++ multithreading asynchronous

13
推荐指数
2
解决办法
2593
查看次数

我何时必须使用boost :: asio:strand

阅读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++ multithreading boost boost-asio

8
推荐指数
2
解决办法
8305
查看次数

条件变量和共享互斥锁

我想知道为什么我不能在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++ multithreading c++14

8
推荐指数
1
解决办法
1710
查看次数