在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++ 覆盖率时,我可以使用 构建我的程序-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
--test
cargo kcov --test failpoints cases::test_normal
cargo 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) 首先,我读了这篇文章,其中说如果没有明确设置,spark.dynamicAllocation.maxExecutors
则其值等于。然而,从本文的以下部分来看,它说“或充当默认值 2 的最小执行器数量”,这让我感到困惑。num-executors
spark.dynamicAllocation.maxExecutors
--num-executors
spark.executor.instances
--num-executors
我的第一个问题是Spark 2.x 或更高版本中的用法是什么?它是否像一个在动态分配之前有用的过时选项?当引入dynamicAllocation时,--num-executors
其--max-executors
行为更像是一些默认值spark.dynamicAllocation.*
?
--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 充当执行器的最小数量”,所以现在出现了矛盾。
num-executors
我的第三个问题是和之间的关系spark.dynamicAllocation.initialExecutors
是spark.dynamicAllocation.initialExecutors
什么num-executors
?从文档中,我发现当num-executors
设置大于时spark.dynamicAllocation.initialExecutors
,它将覆盖spark.dynamicAllocation.initialExecutors
。我想在我的 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)
经过一番搜索 …
macOS 中没有/proc
,但我想获取有关我的系统的信息,例如:
macOS 中有其他替代方案吗?
我们在使用 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) 我写了这样的代码
#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) 我对(>>=)
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
函数也可以做到这一点.有什么理由,还是因为它的设计是这样的?
编辑
从意见,我知道这是很难提取a
的m a
.但是,我认为我可以将monadic m a
视为a
副作用.
是否有可能假设函数m a -> m b …
首先我想谈一下我对此的一些理解,如有错误请指正。
MFENCE
在x86中可以保证全屏障顺序一致性可防止 STORE-STORE、STORE-LOAD、LOAD-STORE 和 LOAD-LOAD 重新排序
这是根据维基百科的说法。
std::memory_order_seq_cst
不保证防止 STORE-LOAD 重新排序。
这是根据Alex 的回答,“负载可能会通过早期存储重新排序到不同位置”(对于 x86),并且 mfence 不会总是被添加。
a是否std::memory_order_seq_cst
表示顺序一致性?根据第2/3点,我认为这似乎不正确。std::memory_order_seq_cst
仅当以下情况时才表示顺序一致性
MFENCE
添加到任一LOAD
或STORE
否则仍有可能重新订购。
根据@LWimsey的评论,我在这里犯了一个错误,如果 和LOAD
都是STORE
,memory_order_seq_cst
则没有重新排序。Alex 可能指出使用非原子或非 SC 的情况。
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) 看了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++ ×5
rust ×3
rust-cargo ×3
apache-spark ×1
c++11 ×1
c++14 ×1
c++17 ×1
constexpr ×1
hadoop-yarn ×1
haskell ×1
kcov ×1
macos ×1
monads ×1
stdatomic ×1
stdoptional ×1
unit-testing ×1
x86 ×1