小编cal*_*vin的帖子

为什么 std::optional::value_or() 采用 U&& 而不是 T&& ?

cppreference上,我们可以看到std::optional采用默认值U&&而不是T&&

它使我无法编写以下代码:

std::optional<std::pair<int, int>> opt;
opt.value_or({1, 2}); // does not compile
opt.value_or(std::make_pair(1, 2)); // compiles
Run Code Online (Sandbox Code Playgroud)

但是,我发现使用 没有任何好处U&&,因为U必须可以转换为T此处。

所以,考虑下面的代码,如果我们有一些U不同于 的类型T,那么就不会有完美的匹配。然而,通过执行隐式转换,我们仍然可以解析我们的调用:

template< class U >
constexpr T value_or( T&& default_value ) const&;
Run Code Online (Sandbox Code Playgroud)

我有以下代码来测试模板函数是否可以接受需要额外隐式转换才能完美匹配的参数,并且它可以编译:

#include <cstdio>
#include <optional>
#include <map>

struct A {
    int i = 1;
};

struct B {
    operator A() const {
        return A{2};
    }
};

template <typename T>
struct C { …
Run Code Online (Sandbox Code Playgroud)

c++ c++17 stdoptional

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

我如何获得货物测试承保?

当我想测试 C++ 覆盖率时,我可以使用 构建我的程序-fprofile-arcs -ftest-coverage,运行所有测试,然后运行gcov以获得覆盖率。

然而,当谈到 Rust 时,我完全迷失了。我想要做的是运行以下测试(在我的 Mac 上),并覆盖路径中的所有 Rust 代码components/raftstore

cargo test --package tests --test failpoints cases::test_normal
cargo test --package tests --test failpoints cases::test_bootstrap
cargo test --package tests --test failpoints cases::test_compact_log
Run Code Online (Sandbox Code Playgroud)

这篇文章来看,它说先运行cargo test --no-run,然后运行kcov。然而,当我真正这样做时,kcov 会永远阻塞。

然后我找到了一个叫“谁提供”的东西 。但是,当我像我所做的那样运行时,我收到错误cargo kcov--testcargo kcov --test failpoints cases::test_normalcargo test

error: cargo subcommand failure
note: cargo test exited with code exit status: 101
error: no test target named `failpoints` …
Run Code Online (Sandbox Code Playgroud)

unit-testing code-coverage rust rust-cargo kcov

13
推荐指数
2
解决办法
6760
查看次数

在YARN上使用Spark时,maxExecutors、num-executors和initialExecutors之间有什么关系?

首先,我读了这篇文章,其中说如果没有明确设置,spark.dynamicAllocation.maxExecutors则其值等于。然而,从本文的以下部分来看,它说“或充当默认值 2 的最小执行器数量”,这让我感到困惑。num-executorsspark.dynamicAllocation.maxExecutors--num-executorsspark.executor.instances

  1. --num-executors我的第一个问题是Spark 2.x 或更高版本中的用法是什么?它是否像一个在动态分配之前有用的过时选项?当引入dynamicAllocation时,--num-executors--max-executors行为更像是一些默认值spark.dynamicAllocation.*

  2. --conf spark.dynamicAllocation.maxExecutors和 和有什么区别--max-executor?后者是否相当于前者的别名?

同时,文章没有提及num-executors和之间的关系spark.dynamicAllocation.initialExecutors。所以我做了一个实验,我发送以下参数:

--conf spark.dynamicAllocation.minExecutors=2  --conf spark.dynamicAllocation.maxExecutors=20  --conf spark.dynamicAllocation.enabled=true --conf spark.dynamicAllocation.initialExecutors=3 --conf spark.dynamicAllocation.maxExecutors=10  --num-executors 0  --driver-memory 1g  --executor-memory 1g  --executor-cores 2  
Run Code Online (Sandbox Code Playgroud)

事实证明,最初分配了 3 个执行器(对应initialExecutors),然后减少到 2 个(对应minExecutors),在这里似乎--num-executors没有用。然而文章中说“-num-executors 或spark.executor.instances 充当执行器的最小数量”,所以现在出现了矛盾。

  1. num-executors我的第三个问题是和之间的关系spark.dynamicAllocation.initialExecutorsspark.dynamicAllocation.initialExecutors什么num-executors?从文档中,我发现当num-executors设置大于时spark.dynamicAllocation.initialExecutors,它将覆盖spark.dynamicAllocation.initialExecutors

hadoop-yarn apache-spark

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

如何解决“阻塞等待构建目录上的文件锁定”问题?

我想在我的 rust 程序中生成一些 .rs 代码,并创建一个gen-proxy-ffi用于bindgen执行此操作的包。下面的命令可以生成我需要的代码。

cargo run --package gen-proxy-ffi --bin gen-proxy-ffi 
Run Code Online (Sandbox Code Playgroud)

生成代码后,我可以通过以下方式构建整个项目

cargo build
Run Code Online (Sandbox Code Playgroud)

现在,我想cargo build通过调用自动生成代码cargo run --package gen-proxy-ffi --bin gen-proxy-ffi。我知道我可以向 build.rs 编写一些代码来指定 rust 的编译例程,因此我编写了以下代码,其中 make-gen-proxy-ffi.sh encaplsules cargo run --package gen-proxy-ffi --bin gen-proxy-ffi

// build.rs
use std::process::Command;
use std::io::Write;

fn main() {
    let o = Command::new("sh").args(&["make-gen-proxy-ffi.sh"]).output().expect("sh exec error!");
}
Run Code Online (Sandbox Code Playgroud)

但是,现在运行时cargo build,会打印出以下错误

+ cargo run --package gen-proxy-ffi --bin gen-proxy-ffi --
    Blocking waiting for file lock on build directory
Run Code Online (Sandbox Code Playgroud)

经过一番搜索 …

rust rust-cargo

6
推荐指数
0
解决办法
1791
查看次数

macOS 中 /proc 有其他选择吗?

macOS 中没有/proc,但我想获取有关我的系统的信息,例如:

  1. /proc/loadavg
  2. /proc/cpu信息
  3. /proc/内存信息
  4. /proc/挂载
  5. /进程/统计

macOS 中有其他替代方案吗?

c++ macos

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

使用 vscode 时如何解决 rust crates“无法进行身份验证”的问题?

我们在使用 Cargo 的时候经常会遇到以下错误。

[ERROR rust_analyzer::lsp_utils] rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /Users/calvin/tiflash/raft-engine/Cargo.toml, Some(Version { major: 1, minor: 64, patch: 0, pre: Prerelease("nightly") }): Failed to run `"cargo" "metadata" "--format-version" "1" "--manifest-path" "/Users/calvin/tiflash/raft-engine/Cargo.toml" "--filter-platform" "x86_64-apple-darwin"`: `cargo metadata` exited with an error:     Updating git repository `https://github.com/pingcap/rust-protobuf`
    Updating git repository `https://github.com/tikv/raft-rs`
error: failed to load source for dependency `raft-proto`

Caused by:
  Unable to update https://github.com/tikv/raft-rs?branch=master

Caused by:
  failed to fetch into: /Users/calvin/.cargo/git/db/raft-rs-42b8049ef2e3af07

Caused by:
  failed to …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo visual-studio-code

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

为什么for循环不是编译时表达式,扩展constexpr允许constexpr函数中的for循环

我写了这样的代码

#include <iostream>
using namespace std;
constexpr int getsum(int to){
    int s = 0;
    for(int i = 0; i < to; i++){
        s += i;
    }
    return s;
}
int main() {
    constexpr int s = getsum(10);
    cout << s << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道它的工作原理是因为扩展了constexpr.然而,在这个问题为什么不是一个for-loop-a-compile-time-expression,作者给出了他的代码如下:

#include <iostream>
#include <tuple>
#include <utility>

constexpr auto multiple_return_values()
{
    return std::make_tuple(3, 3.14, "pi");
}

template <typename T>
constexpr void foo(T t)
{
    for (auto i = 0u; i < …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11 c++14

4
推荐指数
2
解决办法
3823
查看次数

为什么haskell的绑定函数从非monadic到monadic采用函数

我对(>>=)Haskell中绑定函数的定义有一些疑问.

因为Haskell是一种纯语言,所以我们可以使用Monad来处理带副作用的操作.我认为这个策略是有点像把所有的动作可能会引起副作用另一个世界,我们可以从"纯"哈斯克尔世界虽然控制它们do>>=.

所以当我看一下>>=函数的 定义时

(>>=) :: Monad m => m a -> (a -> m b) -> m b
Run Code Online (Sandbox Code Playgroud)

它需要一个(a -> m b)函数,因此m a前一个动作的结果可以"解包"到非monadic a>>=.然后,该功能(a -> m b)需要a作为其输入和返回另一个单子m b作为其结果.通过绑定功能,我可以对monadic进行操作,而不会给纯haskell代码带来任何副作用.

我的问题是为什么我们使用一个(a -> m b)函数?在我看来,一个m a -> m b函数也可以做到这一点.有什么理由,还是因为它的设计是这样的?

编辑

从意见,我知道这是很难提取am a.但是,我认为我可以将monadic m a视为a副作用.

是否有可能假设函数m a -> m b …

monads haskell

4
推荐指数
1
解决办法
569
查看次数

为什么这个“std::atomic_thread_fence”起作用

首先我想谈一下我对此的一些理解,如有错误请指正。

  1. aMFENCE在x86中可以保证全屏障
  2. 顺序一致性可防止 STORE-STORE、STORE-LOAD、LOAD-STORE 和 LOAD-LOAD 重新排序

    这是根据维基百科的说法。

  3. std::memory_order_seq_cst不保证防止 STORE-LOAD 重新排序。

    这是根据Alex 的回答,“负载可能会通过早期存储重新排序到不同位置”(对于 x86),并且 mfence 不会总是被添加。

    a是否std::memory_order_seq_cst表示顺序一致性?根据第2/3点,我认为这似乎不正确。std::memory_order_seq_cst仅当以下情况时才表示顺序一致性

    1. 至少一个显式MFENCE添加到任一LOADSTORE
    2. LOAD(无栅栏)和 LOCK XCHG
    3. LOCK XADD ( 0 ) 和 STORE (无栅栏)

    否则仍有可能重新订购。

    根据@LWimsey的评论,我在这里犯了一个错误,如果 和LOAD都是STOREmemory_order_seq_cst则没有重新排序。Alex 可能指出使用非原子或非 SC 的情况。

  4. std::atomic_thread_fence(memory_order_seq_cst)总是产生一个完整的屏障

    这是根据Alex的回答。所以我总是可以替换asm volatile("mfence" ::: "memory")std::atomic_thread_fence(memory_order_seq_cst)

    这对我来说很奇怪,因为memory_order_seq_cst原子函数和栅栏函数之间的用法似乎有很大不同。

现在我在MSVC 2015的标准库的头文件中找到这段代码,它实现了std::atomic_thread_fence

inline void _Atomic_thread_fence(memory_order _Order)
    {   /* …
Run Code Online (Sandbox Code Playgroud)

c++ x86 memory-barriers stdatomic

4
推荐指数
1
解决办法
2212
查看次数

如何通过直接值初始化避免最烦人的解析?

看了Most vexing parse,明白下面的代码也有歧义

T x();
Run Code Online (Sandbox Code Playgroud)

一方面,它可以被解释为一个函数声明,它返回一个 的对象T。另一方面,它也可以解释为变量定义,对象x值初始化的

我知道我可以像下面的代码一样使用统一初始化来避免冲突:

T x{};
Run Code Online (Sandbox Code Playgroud)

我也明白 ifT是一个(C++11 之前的非POD)类,下面的默认初始化实际上等于值初始化

T x;
Run Code Online (Sandbox Code Playgroud)

同时,如果不需要直接初始化,我们可以使用复制初始化

T x = T();
Run Code Online (Sandbox Code Playgroud)

但是,我认为这三种解决方案中的任何一种都有其局限性。我知道如果有一些参数,我也可以使用额外的一对括号:

T x((arg));
Run Code Online (Sandbox Code Playgroud)

我想采用这个策略,但是下面的代码不起作用

T x(());
Run Code Online (Sandbox Code Playgroud)

是否有一些更好的直接值初始化解决方案?

c++ initialization

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