有一种技术我有时会在覆盖这样的template函数时使用:
#include <utility>
template<int> struct unique_enum { enum class type {}; };
template<int index> using UniqueEnum = typename unique_enum<index>::type;
template<bool b, int index=1>
using EnableFuncIf = typename std::enable_if< b, UniqueEnum<index> >::type;
template<bool b, int index=1>
using DisableFuncIf = EnableFuncIf<!b, -index>;
// boring traits class:
template<typename T>
struct is_int : std::false_type {};
template<>
struct is_int<int> : std::true_type {};
#include <iostream>
// use empty variardic packs to give these two SFINAE functions different signatures:
template<typename C, EnableFuncIf< is_int<C>::value >...>
void …Run Code Online (Sandbox Code Playgroud) 考虑以下C++ 11代码:
struct C {};
void f(int(C));
Run Code Online (Sandbox Code Playgroud)
类型是否f相同:
typedef int T(C);
void f(T);
Run Code Online (Sandbox Code Playgroud)
或者是这样的:
void f(int C);
Run Code Online (Sandbox Code Playgroud)
也就是说,应该将(C)其解释为declarator参数名称的一个C,还是作为abstract-declarator函数参数的一个?
标准中指定的是哪里?
我无法理解如何shared_ptr存储我给它的删除器.
最初,使用a shared_ptr<int>,我认为它可能会使用a std::function<void(int*)>,但我可以作为删除器给出任何类型的函数(或可调用对象),只要第一个参数是a int*.
怎么shared_ptr办呢?
我很抱歉,如果这是一个愚蠢的问题,我是C++的新手,请原谅我!
编辑:问题是:我怎么能做那样的事情?我该怎么用?任何例子?或者这是一个非常高级的话题?
早在2010年,Herb Sutter 就在Dobb博士的一篇文章中提倡使用活性物体而不是裸线.
这是一个C++ 11版本:
class Active {
public:
typedef std::function<void()> Message;
Active(const Active&) = delete;
void operator=(const Active&) = delete;
Active() : done(false) {
thd = std::unique_ptr<std::thread>(new std::thread( [=]{ this->run(); } ) );
}
~Active() {
send( [&]{ done = true; } );
thd->join();
}
void send(Message m) { mq.push_back(m); }
private:
bool done;
message_queue<Message> mq; // a thread-safe concurrent queue
std::unique_ptr<std::thread> thd;
void run() {
while (!done) {
Message msg = mq.pop_front();
msg(); // execute message …Run Code Online (Sandbox Code Playgroud) C++ 11有6种不同的正则表达式语法可供使用.在我的例子中,我正在与使用修改的ECMAScript正则表达式的组件进行交互.
我需要创建一个正则表达式"匹配以X开头的字符串",其中X是我的字符串文字.
所以我想要的正则表达式是粗略的^X.*.除了字符串X可以包含更多的正则表达式特殊字符,我希望它们发生.
这意味着我真的想要^ escaped(X) .*.
现在,我可以阅读ECMAScript文档,找到具有特殊含义的所有字符,编写一个逃避它们的函数,然后完成.但这似乎不优雅,低效且容易出错 - 特别是如果我想支持当前C++支持的所有6种正则表达式,更不用说将来了.
标准中是否有一种简单的方法可以将文字字符串转换为嵌入C++正则表达式,可能是正则表达式语法的函数,还是我必须自己滚动?
下面是使用boost库的类似问题,其中转义列表是硬编码的,然后生成一个反斜杠的正则表达式.我是否已经缩减为适应该答案std?
所以,operator[]不直接说s[s.size()]必须是s[s.size()-1]内存之后的人物.似乎措辞是为了避免提出这种说法.
但是s.data()说s.data()+k == &s[k],并且s.data()必须返回一个指针.
忽略&在CharT上面使用而不是上面使用的看似标准缺陷,std::addressof实现CharT是否s[s.size()]可以在第一次调用之前自由地返回不同的(例如,在受保护的页面上,或在ROM中)s.data()?(显然,它可以将缓冲区安排在一个只读的页面上结束;我说的是另一种情况)
要明确:
据我所知,如果s.data()永远不会被调用(并且编译器可以证明它),那么s[s.size()]就不需要与缓冲区的其余部分连续.
可以在调用之后进行std::addressof(s[s.size()]) 更改,s.data()并且实现符合标准(只要之前s.data()+k == &s[k]已经过.data()评估[],但编译器可以自由执行).或者是否有我看不到的不变性要求?
所以我有一个简单的cow_ptr.它看起来像这样:
template<class T, class Base=std::shared_ptr<T const>>
struct cow_ptr:private Base{
using Base::operator*;
using Base::operator->;
using Base::operator bool;
// etc
cow_ptr(std::shared_ptr<T> ptr):Base(ptr){}
// defaulted special member functions
template<class F>
decltype(auto) write(F&& f){
if (!unique()) self_clone();
Assert(unique());
return std::forward<F>(f)(const_cast<T&>(**this));
}
private:
void self_clone(){
if (!*this) return;
*this = std::make_shared<T>(**this);
Assert(unique());
}
};
Run Code Online (Sandbox Code Playgroud)
这可以保证它拥有一个非常量T并确保它在unique何时出现.write([&](T&){}).
在C++ 17的折旧.unique()似乎表明这种设计是有缺陷的.
我猜测如果我们从线程A中的cow_ptr<int> ptrwith 开始1,将它传递给线程B,使其唯一,修改它2,传ptr回它并在线程中读取它A我们已经生成了竞争条件.
我该如何解决?我可以简单地添加内存屏障write吗?哪一个?或者问题更根本? …
我使用以下命令在 Ubuntu 上安装了 clang-tidy:
sudo apt install clang-tidy
Run Code Online (Sandbox Code Playgroud)
我在一个简单的 C++ 17 文件上运行它,并收到警告和错误:
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:3: warning: 'auto' type specifier is a C++11 extension [clang-diagnostic-c++11-extensions]
auto i = make_unique<int>();
^
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:12: error: use of undeclared identifier 'make_unique' [clang-diagnostic-error]
auto i = make_unique<int>();
Run Code Online (Sandbox Code Playgroud)
我如何告诉 clang-tidy 根据 c++17 标准检查这个文件?
注意:要构建程序,我运行:
clang++-5.0 --std=c++17 ptr.cpp
Run Code Online (Sandbox Code Playgroud) 我的gcc版本4.8.0中的以下代码失败:
#include <thread>
#include <cassert>
int main() {
std::thread::id nobody;
assert( nobody != std::this_thread::get_id() );
};
Run Code Online (Sandbox Code Playgroud)
这种行为是否正确?
编辑原始问题,因为我想问问题std::map,而不是std::vector.我的错.抱歉.
我的数据实际上是2 std::map秒.我想将两张地图合并到第三张地图中.
我的第一张和第二张地图包含具有相同键的条目.所以我想将这些键下的数据合并到我生成的第3张地图中.
所以如果我使用std::merge,我会丢失第一个或第二个地图常用条目中的数据吗?正如我所提到的,两张地图都有共同的数据(值)?