[心得] 2020-2-29 每日新聞

作者: damody (天亮damody)   2020-02-29 15:08:28
#每日新聞
# 2020-2-29 每日新聞
# FB: http://bit.ly/2UizW6X
# C++是不是該有個transform_to函數?
在js叫map的功能
```javascript=
function getOldValues()
{
return ["a", "b", "c", "d"];
}
var newValues = getOldValues().map(v => v.charCodeAt(0));
// result: [97, 98, 99, 100]
```
在C#叫Select的功能
```C#
string[] GetOldValues() => new[] { "a", "b", "c", "d" };
var newValues = GetOldValues().Select(v => (int)v[0]).ToArray();
// result: int[] { 97, 98, 99, 100 };
```
在C++文章作者說他找不到所以自己寫了一個
樓下有人回答他C++20有了
```cpp
auto toCharCode = [](auto&& v) -> int { return v[0]; };
auto oldValues = GetOldValues();
auto newValues = oldValues | std::views::transform(toCharCode);
```
http://bit.ly/32MwmEF
# 現代C++ type trait 快速入門
文章有舉個例
```cpp
void algorithm_signed (int i) { /*...*/ }
void algorithm_unsigned(unsigned u) { /*...*/ }
template <typename T>
void algorithm(T t)
{
if constexpr(std::is_signed<T>::value)
algorithm_signed(t);
else
if constexpr (std::is_unsigned<T>::value)
algorithm_unsigned(t);
else
static_assert(std::is_signed<T>::value || std::is_unsigned<T>::value,
"Must be signed or unsigned!");
}
```
可以透過trait去判斷執行的函數或是編譯失敗
http://bit.ly/2T9NRvq
# C++: 為什麼我使用 references
在C++中使用 pointers 總是伴隨著危險
如果可以,我們應該盡可能的只使用references
下面有使用 references 的優點
如果我不需要更改輸入參數,則將使用const引用。
如果需要該參數的副本,則可以按值接受它並移動它。
如果需要更改輸入參數,我將接受輸入引用。但一般來說,
我更喜歡傳值並返回一個副本。
我避免掉輸出參數。
我使用引用和const引用進行區域命名。
我避免了右值references
http://bit.ly/385Gw4d
# C++20 設計 initializers
C++20中有新的初始化語法
可以針對參數初始化
```cpp
struct bar
{
int x;
};
struct foo
{
int a;
bar b;
char c = 'a';
double d;
};
foo f1{};
// OK: a = 0, b = {x = 0}, c = 'a', d = 0.0
foo f2{ .a = 42 };
// OK: a = 42, b = {x = 0}, c = 'a', d = 0.0
foo f3{ .a = 42, .c = 'b' };
// OK: a = 42, b = {x = 0}, c = 'b', d = 0.0
foo f4{ .a = 42, .b = {.x = 5} };
// OK: a = 42, b = {x = 5}, c = 'a', d = 0.0
foo f5{ .a = 42, .b = {5} };
// OK: a = 42, b = {x = 5}, c = 'a', d = 0.0
```
http://bit.ly/2whEOze
# Bug Ghostcat影響過去13年所有Tomcat
由中國公司Chaitin Tech發現Ghostcat是Tomcat AJP協議中的缺陷。
Chaitin研究人員說,他們在AJP中發現了一個漏洞,
可以利用該漏洞讀取文件或將文件寫入Tomcat服務器。
Ghostcat漏洞廣泛。它會影響所有6.x,7.x,8.x和9.x Tomcat分支。
Apache Tomcat 6.x於2007年2月發布,
這代表過去13年中發布的所有Tomcat版本都容易受到攻擊。
https://zd.net/32zQZUa
# Update4j
Update4j是第一個為Java9+設計的自動更新和啟動器庫。
可以在任何地方輕鬆更新應用程式
(甚至包含Google Drive,Dropbox,Amazon S3,Maven Central)
http://bit.ly/3cjfYQc
# 使用 JDK Flight Recorder 持續觀察程式
JFR是直接內置在Java運行時中的監視和故障排除框架。
JFR可以訪問JVM的所有內部數據,
並且可以以極低的開銷在獲得非常細節的資訊並顯示數據。
http://bit.ly/38bzgDG
# 我要離開 golang
我跟Go語言的蜜月期已經結束。
我已經為該語言投入了數千小時的時間,
並使用了該語言對實作了一些關鍵的基礎設施。
GO有許多優點,編譯連結快速跨平台等等
這邊要講他的缺點
GO的簡單是謊言
一遍又一遍,Go語言的每一份文檔都將其稱為“簡單”。
準確地說,它是半個真相,可以輕鬆地掩蓋一個事實,
當您將某些事情簡化時,便會將複雜性轉移到其他地方。
Golang把複雜性問題隱藏起來但從未解決。
文章的舉例使用 unix 與 windows 檔案文件做舉例
還有檔名包含 utf8 字串等等
還有副檔名的判斷
```
Linux
$ go run main.go
"/" => ""
"/." => "."
"/.foo" => ".foo"
"/foo" => ""
"/foo.txt" => ".txt"
"/foo.txt/bar" => ""
"C:\\" => ""
"C:\\." => "."
"C:\\foo.txt" => ".txt"
"C:\\foo.txt\\bar" => ".txt\\bar"
$ cargo run
作者: plsmaop (plsmaop)   2020-02-29 15:42:00
monkey patching 怎麼在靜態語言做到啊
作者: angle065 (Fu)   2020-02-29 21:14:00
作者: x000032001 (版廢了該走了)   2020-02-29 22:13:00
把page的write 權限打開來改..XD
作者: plsmaop (plsmaop)   2020-02-29 23:35:00
......危險到爆

Links booklink

Contact Us: admin [ a t ] ucptt.com