@EastonMan 看的新闻
+碎碎念
+膜大佬
+偶尔猫猫
+伊斯通听的歌
Daniel Lemire's blog
Passing recursive C++ lambdas as function pointers

In modern C++, as in many popular languages, you can create ‘lambdas’. Effectively, they are potentially anonymous function instances that you can create on the fly as you are programming, possibly inside another function. The following is a simple example.
auto return1 = [](int n) -> int { return 1; };

What about recursive functions? At first I thought you could do…
auto fact = [](int n) -> int {
 if (n == 0) {
   return 1;
 } else {
  return n * fact(n - 1);
 }
};


Sadly it fails. What seems to be happening is that while it recognizes the variable ‘fact’ within the definition of ‘fact’, it cannot use it without knowing its type. So you should specify the type of the ‘fact’ right away. The following will work:
std::function<int(int)> fact = [](int n) -> int {
  if (n == 0) {
    return 1;
  } else {
    return n * fact(n - 1);
  }
};


But using std::function templates may add complexity. For example, what if you have a function that takes a function as a parameter without using std::function, such as…
void print(int(*f)(int)) {
  for(int k = 1; k < 10; ++k) {
   std::cout << "Factorial of " << k << " is " << f(k) << std::endl;
  }
}


Then you would want to call print(fact), but it will not work directly. It may complain like so:
No known conversion from 'std::function' to 'int (*)(int)

So let us avoid the std::function as much as possible:
int (*factorial)(int) = [](int n) -> int {
  if (n == 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
};


And then everything works out fine:
    print(factorial); // OK

Let me finish with a word of caution: functional programming is sophisticated, but it has downsides. One potential downside is performance. Let us consider this conventional code:
int factorialc(int n) {
  if (n == 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}
int functionc() {
  return factorialc(10);
}

Most compilers should produce highly optimized code in such a scenario. In fact, it is likely that the returned value of ‘functionc’ gets computed a compile time. The alternative using lambdas might look as follows:
int (*lfactorial)(int) = [](int n) -> int {
  if (n == 0) {
    return 1;
  } else {
    return n * lfactorial(n - 1);
  }
};

int functionl() {
  return lfactorial(10);
}

Though the results will depend on your system, I would expect far less efficient code in general.

Thus when programming in C++,  if you use lambdas in performance critical code, run benchmarks or disassemble your function to make sure that you have, indeed, zero-cost abstraction.

Credit: Thanks to Ca Yi, Yagiz Nizipli and many X users for informing this post.

source
又有新dmp侧信道纸了
#TIL

https://datatracker.ietf.org/doc/html/rfc8910
https://datatracker.ietf.org/doc/html/draft-ietf-capport-rfc7710bis-11

Captive-Portal Identification in DHCP and Router Advertisements (RAs)

In many environments offering short-term or temporary Internet access (such as coffee shops), it is common to start new connections in a captive portal mode. This highly restricts what the user can do until the user has satisfied the captive portal conditions.

This document describes a DHCPv4 and DHCPv6 option and a Router Advertisement (RA) option to inform clients that they are behind some sort of captive portal enforcement device, and that they will need to satisfy the Captive Portal conditions to get Internet access.
不跳的话得暂停招生
学生跳楼后即可恢复 招生
7年不能毕业的话,就引导学生跳楼?
最好的方法就是
所以对于老师
Easton Meow | 是满满
跳楼是否影响招生
应该不影响,影响的话清华早就没学生了(?
跳楼是否影响招生
Back to Top