小编cal*_*vin的帖子

C++0x 中的类型别名有什么替代方法吗?

我想arrstd::array<T, 32>.

template<typename T>
using arr = std::array<T, 32>;
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于仅支持 C++0x(无类型别名)的 GCC 4.4.6。

我认为现在使用 GCC 4.4.6 是一个非常糟糕的主意,但是,我想知道是否有一些方法可以模拟type alias

以下代码可能有效,但arr不准确std::array<T, 32>,所以我想要一个更好的解决方案。

template<typename T>
struct arr : public std::array<T, 32>;
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 c++03

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

为什么我们使用 `Box::pin` 来取消固定,使用 `Pin::new` 来取消固定?

在学习 Rust 中 Pin 的实现时,我感到很困惑。

我知道当一个类型不能安全移动时,它可以 impl !UnpinPin就是防止别人得到&mut T这些类型,让他们不能得到std::mem::swap

如果 Pin 是为!Unpin类型设计的,为什么我们不能直接调用Pin::new这些类型呢?

它给出如下错误。我知道我应该使用 Pin::new_unchecked,但为什么呢?

struct TestNUnpin {
    b: String,
}
impl !Unpin for TestNUnpin {}

// error: the trait `Unpin` is not implemented for `TestNUnpin`
std::pin::Pin::new(&TestNUnpin{b: "b".to_owned()});
Run Code Online (Sandbox Code Playgroud)

我的推理是:

  1. 固定是为了帮助!取消固定类型
  2. 我们可以将 !Unpin 类型传递给 Pin::new 以使它们不可移动。
  3. 对于 Unpin 类型,它们不能被 pinned,所以我们不能通过 Pin::new 创建

rust

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

为什么货物安装需要“--locked”才能工作?

我试图强行安装cargo-sort

cargo install --force cargo-sort
Run Code Online (Sandbox Code Playgroud)

然而,它抱怨

error: failed to compile `cargo-sort v1.0.9`, intermediate artifacts can be found at `/var/folders/2q/sfkj3vg13z175flb8j9znf9w0000gn/T/cargo-installJcyWnG`

Caused by:
  package `clap_builder v4.4.0` cannot be built because it requires rustc 1.70.0 or newer, while the currently active rustc version is 1.67.0-nightly
  Try re-running cargo install with `--locked
Run Code Online (Sandbox Code Playgroud)

我的 Rust 工具链是

nightly-2022-11-15-x86_64-apple-darwin (override)
Run Code Online (Sandbox Code Playgroud)

因为它建议我使用--locked. 所以我用

cargo install --force --locked cargo-sort
Run Code Online (Sandbox Code Playgroud)

有效。

我的 Cargo.lock 是https://github.com/pingcap/tidb-engine-ext/blob/raftstore-proxy/Cargo.lock

从 rust 的文档中,我知道--locked意味着使用 Cargo.lock 中所需的版本。clap_builder但是,我发现我的 Cargo.lock 中没有任何条目。

我的问题是为什么 …

rust rust-cargo

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

当“*this”无效时,为什么 std::Optional 中的“operator-&gt;”会导致 UB?

根据 Cppreference,optional::value()onstd::nullopt会抛出异常,而解引用an optionalwhich isstd::nullopt是 UB。

我有测试,似乎UB在某些编译器中可能不会panic,这比panic更可怕,所以如果错误使用它是非常有害的。我并不是说 UB 是好的或有意义的,但是,它是一个错误,如果发生的话,错误的结果比恐慌更难调试。

struct I {
    int get() { return 1; }
};

void f() {
    try {
        std::optional<std::shared_ptr<I>> x = std::nullopt;
        printf("%d\n", (*x)->get()); // -----> UB
    }
    catch (std::bad_optional_access & e) {
        printf("it throws in f\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么std::optional设计为导致 UB 而不是像/value()那样抛出异常?operator*operator->

c++

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

flex 如何一次返回多个终端

为了使我的问题易于理解,我想使用以下示例:

以下代码在fortran语言中称为非块do-loop

DO 20 I=1, N      ! line 1
DO 20 J=1, N      ! line 2
    ! more codes
20  CONTINUE      ! line 4
Run Code Online (Sandbox Code Playgroud)

要注意的是,标签204个装置的末端内做环和外DO循环。

我希望我的 flex 程序正确解析该功能:当 flex 读取 label 时20,它将返回ENDDO终端两次。

首先,因为我也用bison,所以每次bison 打电话yylex()都拿到一个终端。如果我可以yylex()在某些情况下要求 bison 获取终端,在其他情况下从另一个函数获取终端,也许我可以解决这个问题,但是,我当时不知道。

当然有一些解决方法,例如,我可以使用 flex 的启动条件,但我认为这不是一个好的解决方案。所以我问是否有任何方法可以在没有解决方法的情况下解决我的问题?

yacc lex compilation bison flex-lexer

2
推荐指数
1
解决办法
1927
查看次数

为什么我不能在 bash 脚本中使用 `source` ssh-agent 和 ssh-add ?

当我尝试时git push,出现以下错误。

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

我知道我应该在这里指定我自己的密钥,所以我在 bash 窗口中输入以下内容,它工作正常。

ssh-agent bash
ssh-add ~/.ssh/id_rsa_lrz
git push
Run Code Online (Sandbox Code Playgroud)

但是,我想让事情变得简单,所以我有一个像这样的 set_env.sh

ssh-agent bash
ssh-add ~/.ssh/id_rsa_lrz
Run Code Online (Sandbox Code Playgroud)

我像这样在 bash 窗口中输入

. set_env.sh
git push
Run Code Online (Sandbox Code Playgroud)

但是,我ERROR: Repository not found.再次收到错误,但为什么呢?

ssh bash ssh-agent

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

如何使用基类的 std::hash 对类型进行哈希处理?

我有一个C<B>继承自 的类模板std::string,我想让它像普通的 一样可散列std::string。我编写了以下代码,并且可以编译。

我想知道这是否是正确的实现。由于从派生类到基类的转换可能会截断内存,我想知道它是否会花费太多?

#include <unordered_set>
#include <string>

template <bool B>
struct C : std::string
{
    C(std::string s) : std::string(s) {}
};

namespace std {

    template <bool B>
    struct hash<C<B>>
    {
        std::size_t operator()(const C<B> &k) const {
            return std::hash<std::string>()(static_cast<std::string>(k));
        }
    };

} // namespace std

int main() {
    std::unordered_set<C<false>> s;
    std::string c = C<false>("a");
    s.insert(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization stdhash

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

如何在 Scala 中从 String 转换 Long

我知道我可以解析LongString类似下面

"60".toLong
Run Code Online (Sandbox Code Playgroud)

LongDouble如下转换

60.0.toLong
Run Code Online (Sandbox Code Playgroud)

或转换Long来自一个StringDouble类似于下面

"60.0".toDouble.toLong
Run Code Online (Sandbox Code Playgroud)

但是,我不能执行以下操作

"60.0".toLong
Run Code Online (Sandbox Code Playgroud)

所以我的问题是使用是否.toDouble.toLong是最佳实践,还是应该使用类似的东西try ... catch ...

同时,还有一个问题,当我尝试将一个非常大的转换Long为 时Double,可能会有一些精度损失,我想知道如何解决这个问题?

"9223372036854775800.31415926535827932".toDouble.toLong
Run Code Online (Sandbox Code Playgroud)

scala

0
推荐指数
1
解决办法
70
查看次数

为什么我在 ip 而不是主机名上卷曲时需要额外的“-k”标志?

我有这个代码

$ curl  "https://api.weixin.qq.com:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code"
Run Code Online (Sandbox Code Playgroud)

它可以从服务器获得如下响应

{"errcode":40013,"errmsg":"invalid appid rid: 5f96703e-5e24e4f1-691a9221"}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将主机名替换为 ip 时,我失败并显示以下消息

$ host api.weixin.qq.com
api.weixin.qq.com has address 180.97.7.108
api.weixin.qq.com has address 101.226.212.27

$ curl  "https://101.226.212.27:443/sns/jscode2session?appid=a&secret=b&js_code=c&grant_type=authorization_code"
curl: (60) SSL: no alternative certificate subject name matches target host name '101.226.212.27'
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above. …
Run Code Online (Sandbox Code Playgroud)

https curl http

0
推荐指数
1
解决办法
370
查看次数

为什么 std::thread::spawn 不能接受 Rust 中的参数?

在C++中,我们可以编写如下代码

void f1(int n)
{
    
}

std::thread t(f1, 1);
Run Code Online (Sandbox Code Playgroud)

然而,在 Rust 中我只能通过闭包来做到这一点,并且它将涉及捕获。

fn foo(b: i32) {

}


let bb = 1;
let t1 = std::thread::spawn(move || foo(bb));
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一些方法可以直接创建一个运行的线程foo,并bb直接作为其参数传递,就像在 C++ 中一样。

如果没有办法做到这一点,这是设计使然吗?有人说关闭它糖,但是我发现它在这里不能替代?

rust

0
推荐指数
1
解决办法
1030
查看次数