小编bob*_*eff的帖子

decltype行为背后的理由是什么?

正如我在C++ 11 decltype(expression)中所理解的,用于推断给定表达式的完全相同类型.但是当表达式放入括号本身时,则推导类型是对表达式类型的左值引用.例如:

int x;
decltype(x) y = x;
Run Code Online (Sandbox Code Playgroud)

相当于int y = x;但是,

int x;
decltype((x)) y = x;
Run Code Online (Sandbox Code Playgroud)

相当于int& y = x;.

分别

 decltype(auto) f1()
 {
   int x = 0;
   return x; // decltype(x) is int, so f1 returns int
 }
Run Code Online (Sandbox Code Playgroud)

 decltype(auto) f2()
 {
   int x = 0;
   return (x); // decltype((x)) is int&, so f2 returns int&
 }
Run Code Online (Sandbox Code Playgroud)

标准委员会选择这种行为的理由是什么?

后记:

现在我观察到,至少在GCC 6.2实现的情况下,当括号中的表达式更复杂时,例如decltype((x + x))推导的类型是T,但不是 …

c++ decltype c++11 type-deduction c++14

57
推荐指数
3
解决办法
2010
查看次数

std :: shared_ptr和std :: experimental :: atomic_shared_ptr有什么区别?

我阅读以下通过文章安东尼威廉姆斯和我除了理解为原子共享计数std::shared_ptrstd::experimental::atomic_shared_ptr实际指针到共享对象也是原子?

但是,当我读到的引用计数的版本lock_free_stack在安东尼的书中描述了关于C++并发似乎对我来说,同样aplies也是std::shared_ptr,因为功能,如std::atomic_load,std::atomic_compare_exchnage_weak被应用到的实例std::shared_ptr.

template <class T>
class lock_free_stack
{
public:
  void push(const T& data)
  {
    const std::shared_ptr<node> new_node = std::make_shared<node>(data);
    new_node->next = std::atomic_load(&head_);
    while (!std::atomic_compare_exchange_weak(&head_, &new_node->next, new_node));
  }

  std::shared_ptr<T> pop()
  {
    std::shared_ptr<node> old_head = std::atomic_load(&head_);
    while(old_head &&
          !std::atomic_compare_exchange_weak(&head_, &old_head, old_head->next));
    return old_head ? old_head->data : std::shared_ptr<T>();
  }

private:
  struct node
  {
    std::shared_ptr<T> data;
    std::shared_ptr<node> next;

    node(const T& data_) : data(std::make_shared<T>(data_)) {} …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency smart-pointers atomic c++11

21
推荐指数
3
解决办法
6909
查看次数

在MSVC 2017下缺少C11 strerrorlen_s功能

我正试图在MSVC 2017下找到C11标准中包含的strerrorlen_s功能头.我需要它来为错误消息分配空间.代码如下:strerror_s

auto size = strerrorlen_s(errno) + 1;
char* errorReason = (char*)alloca(size);
strerror_s(errorReason, size, errno);
std::ostringstream oss;
oss << "Cannot open: " << fileName << " Reason: " << errorReason;
throw std::runtime_error(oss.str());
Run Code Online (Sandbox Code Playgroud)

在文档中有以下字样:

与所有边界检查函数一样,如果__STDC_LIB_EXT1__由实现定义并且用户在包含之前定义__STDC_WANT_LIB_EXT1__整数常量1,则仅保证strerror_s和strerrorlen_s可用string.h.

MSVC 2017没有定义__STDC_LIB_EXT1__,似乎__STDC_WANT_LIB_EXT1__在包含之前定义string.h没有效果.虽然strerror_s可以.

  • strerrorlen_s在现有的WindowsMSVC 2017年
  • 如果函数不可用,是否有可能通过其他方式获取错误消息长度?
  • strerror_sWindows下是否是线程安全的,因为在Linux下它似乎不是并且如果需要线程安全必须使用strerror_r,但它在Windows上不可用?

c++ tr24731 visual-studio

13
推荐指数
1
解决办法
2230
查看次数

LZ4库解压缩数据上限大小估计

我正在使用LZ4库并在使用时解压缩数据

int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
Run Code Online (Sandbox Code Playgroud)

我想估计最大解压缩数据大小.但我找不到反向功能了

int LZ4_compressBound(int isize);
Run Code Online (Sandbox Code Playgroud)

我可以用它来确定解压缩数据的上限,这maxDecompressedSize是解压缩函数的最后一个参数.

其他压缩库(例如snappy)提供了这样的功能.

bool GetUncompressedLength(Source* source, uint32* result);
Run Code Online (Sandbox Code Playgroud)

如果我无法保存初始数据大小(在压缩之前),并且如果我不想对我必须分配的缓冲区大小过度悲观,我该怎么办?

c compression snappy lz4

9
推荐指数
2
解决办法
3123
查看次数

并行for_each比std :: for_each慢两倍多

我在读C++并发在行动安东尼·威廉姆斯.在关于设计并发代码的章节中,有std :: for_each algorihtm的并行版本.以下是本书略有修改的代码:

join_thread.hpp

#pragma once

#include <vector>
#include <thread>

class join_threads
{
public:
  explicit join_threads(std::vector<std::thread>& threads)
    : threads_(threads) {}

  ~join_threads()
  {
    for (size_t i = 0; i < threads_.size(); ++i)
    {
      if(threads_[i].joinable())
      {
        threads_[i].join();
      }
    }
  }

private:
  std::vector<std::thread>& threads_;
};
Run Code Online (Sandbox Code Playgroud)

parallel_for_each.hpp

#pragma once

#include <future>
#include <algorithm>

#include "join_threads.hpp"

template<typename Iterator, typename Func>
void parallel_for_each(Iterator first, Iterator last, Func func)
{
  const auto length = std::distance(first, last);
  if (0 == …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm parallel-processing concurrency foreach

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

PostgreSQL 错误:有效负载字符串太长

我有以下 PostgreSQL 表和触发器函数实现表的历史记录:

CREATE TABLE "ps_counters"
(
  "psid" integer NOT NULL,
  "counter" bigint[] NOT NULL -- This is the array of 256 counters. Upon insertion of new PS all these values must set to array of 256 bigint elements all equal to 0.
);

CREATE TABLE "ps_counters_history"
(
  "id" serial PRIMARY KEY,
  "timestamp" timestamp NOT NULL DEFAULT clock_timestamp(),
  "psid" integer NOT NULL,
  "counter" bigint[] NOT NULL
);

CREATE OR REPLACE FUNCTION ps_counters_history_trigger()
  RETURNS trigger AS
$BODY$
  DECLARE
    table_name text;
  BEGIN …
Run Code Online (Sandbox Code Playgroud)

sql postgresql triggers

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

如何配置leiningen使用最新的Clojure版本进行repl在项目之外启动?

当我开始Clojure的REPL具体项目,为leiningen足以在具体指定正确的Clojure的版本project.clj所描述的文件在这里.但是当我在项目之外开始repl时,旧的版本就开始了.在我的情况下,旧版本是1.5.1,我想升级到1.6.0.这里提出了不工作的解决方案,但是在评论中,据说在lieingen版本2.1中问题得到了解决.我使用leiningen的2.3.4版本,但建议的解决方案对我不起作用.

clojure leiningen

7
推荐指数
1
解决办法
1821
查看次数

这个boost :: asio和boost :: coroutine使用模式有什么问题?

这个问题中,我描述了boost :: asioboost :: coroutine使用模式,它导致我的应用程序随机崩溃,我发布了我的代码和valgrind以及GDB输出的提取.

为了进一步研究这个问题,我创建了一个较小的概念证明应用程序,它应用了相同的模式.我看到在我发布的源代码较小的程序中出现了同样的问题.

代码启动几个线程并创建一个带有几个虚拟连接的连接池(用户提供的数字).其他参数是无符号整数,它们扮演虚假请求的角色.sendRequest函数的虚拟实现只是启动异步计时器,等待输入数字的等待秒数和函数的yileds.

有人可以看到这个代码的问题,他可以提出一些解决方案吗?

#include "asiocoroutineutils.h"
#include "concurrentqueue.h"

#include <iostream>
#include <thread>

#include <boost/lexical_cast.hpp>

using namespace std;
using namespace boost;
using namespace utils;

#define id this_thread::get_id() << ": "

// ---------------------------------------------------------------------------

/*!
 * \brief This is a fake Connection class
 */
class Connection
{
public:
    Connection(unsigned connectionId)
        : _id(connectionId)
    {
    }

    unsigned getId() const
    {
        return _id;
    }

    void sendRequest(asio::io_service& …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading valgrind boost-asio boost-coroutine

6
推荐指数
1
解决办法
1122
查看次数

使用 boost::asio stackless 协程通过 HTTP 下载多个文件

我翻译的例子,从在Lua编程罗伯托·萨利姆斯使用协同程序,以C ++使用通过HTTP下载几个文件的boost :: ASIO和stackful协同程序。这是代码:

#include <iostream>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

using namespace std;
using namespace boost::asio;

io_service ioService;

void download(const string& host, const string& file, yield_context& yield)
{
  clog << "Downloading " << host << file << " ..." << endl;

  size_t fileSize = 0;
  boost::system::error_code ec;

  ip::tcp::resolver resolver(ioService);

  ip::tcp::resolver::query query(host, "80");
  auto it = resolver.async_resolve(query, yield[ec]);

  ip::tcp::socket socket(ioService);
  socket.async_connect(*it, yield[ec]);

  ostringstream req;
  req << "GET " << file << " HTTP/1.0\r\n\r\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ boost coroutine boost-asio

6
推荐指数
1
解决办法
827
查看次数

我可以检索用柯南包管理器打包的库的源代码以便能够在其中进行调试吗?

通常,Conan包只包含构建工件,如*.dll*.lib*.pdb*.so*.a*.dylib文件以及给定CC++库的头文件。然而,有时当您调试使用库的代码时,能够进入库代码以查看内部发生的情况非常有用。例如,确定何时出现问题是由于库的不正确使用还是由于其中的错误。

  1. 是否可以与您使用的包一起检索构建它的源代码,以便能够在其中进行调试?
  2. 如果这对于任意包是不可能的,是否可以自己创建这样的包?

c c++ packages conan

6
推荐指数
1
解决办法
1754
查看次数