正如我在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,但不是 …
我阅读以下通过文章安东尼威廉姆斯和我除了理解为原子共享计数std::shared_ptr在std::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) 我正试图在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在现有的Windows与MSVC 2017年?strerror_s在Windows下是否是线程安全的,因为在Linux下它似乎不是并且如果需要线程安全必须使用strerror_r,但它在Windows上不可用?我正在使用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++并发在行动由安东尼·威廉姆斯.在关于设计并发代码的章节中,有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) 我有以下 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) 在这个问题中,我描述了boost :: asio和boost :: 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) 我翻译的例子,从在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) 通常,Conan包只包含构建工件,如*.dll、*.lib、*.pdb、*.so、*.a、*.dylib文件以及给定C或C++库的头文件。然而,有时当您调试使用库的代码时,能够进入库代码以查看内部发生的情况非常有用。例如,确定何时出现问题是由于库的不正确使用还是由于其中的错误。