小编Seo*_*ong的帖子

如何在C++中使用Null Lambda?

我想声明一个这样的函数:

template <typename Lambda>
int foo(Lambda bar) {
    if(/* check bar is null lambda */)
        return -1;
    else
        return bar(3);
}

int main() {
    std::cout << foo([](int a)->int{return a + 3;}) << std::endl;
    std::cout << foo(NULL_LAMBDA) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

然后,我如何声明NULL_LAMBDA和条件检查传递lambda函数是否为null?

c++ lambda c++11

17
推荐指数
2
解决办法
3054
查看次数

为什么rvalue引用绑定到xvalue在我的代码中不起作用?

我试图理解C++ 11中的左值和右值.所以我写了一个测试代码:

int x = 10;
int foo() { return x; }
int& bar() { return x; }
int&& baz() { return 10; }

int main() {
    int& lr1 = 10;     // error: lvalue references rvalue
    int& lr2 = x;      // ok
    int& lr3 = foo();  // error: lvalue references rvalue
    int& lr4 = bar();  // ok
    int& lr5 = baz();  // error: lvalue references rvalue

    int&& rr1 = 10;    // ok
    int&& rr2 = x;     // error: rvalue references …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue lvalue xvalue c++11

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

WSL2 上的 apt-get 更新失败

我安装了 WSL2 ubuntu 18.04 并尝试运行apt-get update命令。然后出现以下日志。

seokmin@DESKTOP-XXXXXX:~$ sudo apt-get update
[sudo] password for seokmin:
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::24). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::23). - connect (101: Network is unreachable) Could not connect to archive.ubuntu.com:80 (91.189.88.142), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.152), connection timed out
Err:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::24). - connect (101: Network is …
Run Code Online (Sandbox Code Playgroud)

wsl-2

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

如何在 SML 中“运行”零参数函数

如果有这样的函数foo

fun foo() = 10
Run Code Online (Sandbox Code Playgroud)

当我想运行foo 时,我是这样写的:

val bar = foo
Run Code Online (Sandbox Code Playgroud)

但是bar变成了一个函数 (unit->int) 而不是 10

所以我试过这样:

val int bar = foo
Run Code Online (Sandbox Code Playgroud)

但它会出错。所以我试过这样:

fun foo x = 10

val bar = foo 0
Run Code Online (Sandbox Code Playgroud)

最后我得到bar = 10,但它看起来很糟糕。

那么,我应该怎么做才能在不改变任何东西的情况下运行零参数函数?

function sml

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

为什么在继承的标准布局规则中有非静态成员的规则?

我读了一篇关于POD,Trivial,Standard-layout类的非常好的文章.但我对标准布局类的规则有疑问:

要么在大多数派生类中没有非静态数据成员,要么最多只有一个具有非静态数据成员的基类,或者没有带有非静态数据成员的基类

我写了一个源代码:

#include <iostream>

struct A {
    int a;
};

struct B {
    int b;
};

struct C : A, B {
    int c;
};

int main() {
    C c = {}; // initialize C
    c.a = 0xffffffff;
    c.b = 0xeeeeeeee;
    c.c = 0xdddddddd;
    std::cout << std::hex << *(reinterpret_cast<int*>(&c)  ) << std::endl;
    std::cout << std::hex << *(reinterpret_cast<int*>(&c)+1) << std::endl;
    std::cout << std::hex << *(reinterpret_cast<int*>(&c)+2) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

ffffffff
eeeeeeee
dddddddd
Run Code Online (Sandbox Code Playgroud)

我觉得它运作得很好.在VS2015中使用调试器,它看起来很好.

在此输入图像描述

那么,为什么在继承的标准布局规则中有非静态成员的限制?

c++ pod standard-layout c++11

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

标签 统计

c++ ×3

c++11 ×3

function ×1

lambda ×1

lvalue ×1

pod ×1

rvalue ×1

sml ×1

standard-layout ×1

wsl-2 ×1

xvalue ×1