小编Val*_*lea的帖子

unique_ptr线程安全吗?

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)

c++ multithreading thread-safety unique-ptr c++11

26
推荐指数
3
解决办法
1万
查看次数

IntelliJ调用字段的层次结构

Eclipse JDT具有"调用层次结构"功能 - 从字段/方法开始,它以递归方式查找所有引用.

IntelliJ也实现了这一点,但它只适用于方法.对于字段,您只能"查找用法",因此如果您想深入挖掘,则必须进行其他搜索.

试过'数据流到这里',但这不是我想要的.

我错过了什么吗?有没有更好的方法来探索IntelliJ中的字段使用?

eclipse intellij-idea call-hierarchy

24
推荐指数
2
解决办法
4918
查看次数

为什么返回参数时不允许使用RVO?

它在[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的生命周期吗?

c++ return-value-optimization copy-elision rvo

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

MSVC中的ODR错误?

打印此程序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)

c++ one-definition-rule visual-c++

20
推荐指数
1
解决办法
325
查看次数

创建一个在Ruby中使用额外参数的setter方法

我正在尝试编写一个充当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)

为什么这不起作用,我错过了显而易见的事吗?

ruby methods setter indexer

18
推荐指数
2
解决办法
3376
查看次数

将lambda分配给std :: function

当推断的返回类型是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)

c++ lambda implicit-conversion c++11 std-function

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

从基类访问联合的公共部分

我有一个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)

c++ strict-aliasing c++11

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

bash - 检查字符串是否包含换行符

检查字符串是否包含换行符的最简单方法是什么?

例如,之后

FILE=$(find . -name "pattern_*.sh")
Run Code Online (Sandbox Code Playgroud)

我想检查换行符以确保只匹配一个文件.

unix bash newline

5
推荐指数
3
解决办法
1万
查看次数

以最小的努力从noexcept中获利

我理解容器可以优化具有noexcept移动构造函数/赋值/交换的类型.但是,是否有实际的原因(除了文档)指定其他操作noexcept

具体来说,我感兴趣的是,如果标准库中有优化,还可以通过以下方式获得noexcept:

  1. 默认构造函数
  2. 复制构造函数
  3. 复制作业

c++ noexcept c++11

5
推荐指数
0
解决办法
99
查看次数