unique_ptr线程安全吗?以下代码是否无法两次打印相同的数字?
#include <memory>
#include <string>
#include <thread>
#include <cstdio>
using namespace std;
int main()
{
unique_ptr<int> work;
thread t1([&] {
while (true) {
const unique_ptr<int> localWork = move(work);
if (localWork)
printf("thread1: %d\n", *localWork);
this_thread::yield();
}
});
thread t2([&] {
while (true) {
const unique_ptr<int> localWork = move(work);
if (localWork)
printf("thread2: %d\n", *localWork);
this_thread::yield();
}
});
for (int i = 0; ; i++) {
work.reset(new int(i));
while (work)
this_thread::yield();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) Eclipse JDT具有"调用层次结构"功能 - 从字段/方法开始,它以递归方式查找所有引用.
IntelliJ也实现了这一点,但它只适用于方法.对于字段,您只能"查找用法",因此如果您想深入挖掘,则必须进行其他搜索.
试过'数据流到这里',但这不是我想要的.
我错过了什么吗?有没有更好的方法来探索IntelliJ中的字段使用?
它在[C++ 11:12.8/31]中说明:
复制/移动操作的省略,称为复制省略,允许[...]:
- 在具有类返回类型的函数的return语句中,当表达式是具有与函数返回类型相同的cv-unqualified类型的非易失性自动对象(除函数或catch子句参数之外)的名称时,通过将自动对象直接构造到函数的返回值中,可以省略复制/移动操作
这意味着
#include <iostream>
using namespace std;
struct X
{
X() { }
X(const X& other) { cout << "X(const X& other)" << endl; }
};
X no_rvo(X x) {
cout << "no_rvo" << endl;
return x;
}
int main() {
X x_orig;
X x_copy = no_rvo(x_orig);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将打印
X(const X& other)
no_rvo
X(const X& other)
Run Code Online (Sandbox Code Playgroud)
为什么需要第二个拷贝构造函数?编译器不能简单地延长x的生命周期吗?
打印此程序1 1
而不是1 2
使用MSVC编译时(直到VS 2015).
f1.cpp:
#include <functional>
static std::function<int ()> helper() {
struct F { int operator()() { return 1; } };
return F();
}
std::function<int ()> f1() { return helper(); }
Run Code Online (Sandbox Code Playgroud)
f2.cpp:
#include <functional>
static std::function<int ()> helper() {
struct F { int operator()() { return 2; } };
return F();
}
std::function<int ()> f2() { return helper(); }
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <functional>
#include <iostream>
std::function<int ()> f1();
std::function<int ()> f2();
int main() {
std::cout << …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个充当setter的方法,除了赋值之外还需要一些额外的参数.愚蠢的例子:
class WordGenerator
def []=(letter, position, allowed)
puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}"
end
def allow=(letter, position, allowed)
# ...
end
end
Run Code Online (Sandbox Code Playgroud)
把它写成索引器是有效的,我可以像这样调用它:
gen = WordGenerator.new
gen['a', 1] = true
# or explicitly:
gen.[]=('a', 1, true)
Run Code Online (Sandbox Code Playgroud)
但当我尝试以下任何一种情况时,口译员会抱怨:
gen.allow('a', 1) = false # syntax error
gen.allow=('a', 1, false) # syntax error
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用,我错过了显而易见的事吗?
当推断的返回类型是std :: nullptr_t时,为什么允许第二个赋值?使用函数指针,这是禁止的.
为什么第二个lambda不运行?
#include <cstdio>
#include <functional>
int main()
{
std::function<void* ()> f;
f = []() -> void* {
printf ("runs\n");
return nullptr;
};
f();
f = []() {
printf ("doesn't run\n");
return nullptr; // -> std::nullptr_t
};
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个Result<T>
模板类,它包含一些error_type
和的联合T
.我想在不借助虚函数的情况下公开基类中的公共部分(错误).
这是我的尝试:
using error_type = std::exception_ptr;
struct ResultBase
{
error_type error() const
{
return *reinterpret_cast<const error_type*>(this);
}
protected:
ResultBase() { }
};
template <class T>
struct Result : ResultBase
{
Result() { new (&mError) error_type(); }
~Result() { mError.~error_type(); }
void setError(error_type error) { mError = error; }
private:
union { error_type mError; T mValue; };
};
static_assert(std::is_standard_layout<Result<int>>::value, "");
void check(bool condition) { if (!condition) std::terminate(); }
void f(const ResultBase& alias, Result<int>& r)
{ …
Run Code Online (Sandbox Code Playgroud) 检查字符串是否包含换行符的最简单方法是什么?
例如,之后
FILE=$(find . -name "pattern_*.sh")
Run Code Online (Sandbox Code Playgroud)
我想检查换行符以确保只匹配一个文件.
我理解容器可以优化具有noexcept移动构造函数/赋值/交换的类型.但是,是否有实际的原因(除了文档)指定其他操作noexcept
?
具体来说,我感兴趣的是,如果标准库中有优化,还可以通过以下方式获得noexcept
:
c++ ×6
c++11 ×4
bash ×1
copy-elision ×1
eclipse ×1
indexer ×1
lambda ×1
methods ×1
newline ×1
noexcept ×1
ruby ×1
rvo ×1
setter ×1
std-function ×1
unique-ptr ×1
unix ×1
visual-c++ ×1