changkun / modern-cpp-tutorial Goto Github PK
View Code? Open in Web Editor NEW📚 Modern C++ Tutorial: C++11/14/17/20 On the Fly | https://changkun.de/modern-cpp/
Home Page: https://changkun.de/modern-cpp/
License: MIT License
📚 Modern C++ Tutorial: C++11/14/17/20 On the Fly | https://changkun.de/modern-cpp/
Home Page: https://changkun.de/modern-cpp/
License: MIT License
运算表达式产生的临时变量、 原始字面量、Lambda 表达式都属于纯右值。
原始字面量并不一定是右值,比如 字符字面量,字符字面量可以被取地址,是一个左值。
参见:https://en.cppreference.com/w/cpp/language/value_category
Hi
这里有错别字
第二章的第一道习题没太明白是什么意思,然后我想看看答案,结果答案是404。
Please briefly describe your motivation.
Please list all of your suggestions.
请描述你提交内容建议的动机。
请描述你提交内容建议的详单,例如具体是增加哪个知识点的说明。
e005c2e#diff-ee1f22d0bfaa5a21e1ee8a4506e6a8f9R18
O
unexpected_handler
X
unexcepted_handler
README.md
& CONTRIBUTING.md
Some of the translations in this file can be improved:
After checking #12, I saw a lot of good work was done, and in #87 some extra work is done, but I'd love to help out, starting with the README and CONTRIBUTING
modern-cpp-tutorial/book/02-usability.md
Line 26 in 8317c2b
违反与直观搭配似乎不太合适
违反 直觉
违反(直观的)印象
modern-cpp-tutorial/book/02-usability.md
Line 114 in 8317c2b
回 / 会
modern-cpp-tutorial/book/02-usability.md
Line 719 in 8317c2b
以下 / 一下
This repo has supported many different types of reading approach.
It is little bit tragedy because it does not support CI deployment yet.
I am current suffering manually deploy the website to my private server.
Therefore it seems necessary to set up automatic CI service into this repo.
, 让编译器执行推导应用列表
, 让编译器执行推导引用列表
必要时,请附上相关截图
p8 amzed -> amazed?
p10 Deprecation is not completely unusable, it is only intended to imply that programmers
will disappear from future standards and should be avoided.
-> I am not a native English speaker, but I think "programmers" above should be
"features" or something like this.
p10 cpp char *str = "hello world!"; // A deprecation warning will appear
-> Isn't "cpp" unnecessary (and wrong)?
p12 (CXX)(SOURCE_CXX) (OBJECT SC )(LDFLAGS_COMMON) -o (TARGET)clean : rm − rf ∗ .o(
TARGET) -> I think "clean" make target should be on a separate line. Plus, $ should
prefix the parentheses, like $(CXX).
p16 so even if this behavior is in most compilers Both support, but
-> so even if this behavior is supported by most compilers, but
p16 This keyword explicitly tells the compiler that it should verify that len_foo should be
a compile time. Constant expression. -> a compile time constant expression.
p17 which causes us to rename the other when we need to traverse the entire std::vectors
again. A variable. -> I guess "A variable" should be deleted or appended earlier "rename the other
variable when".
p18 which is The initialization of class -> which is the initialization of class
(Note that this kind of abrupt capitalisations are fairly common.)
p32 Line 15 // inhereit constructor => inheritance constructor
p36 but you don’t want to use a function to name a function -> I guess the following is
better/more correct: "but you don’t want to use name to call a function"
p36 for the things in [catch list] -> for the things in [capture list]
p39 Although this part of the standard library is part of the standard library =>
I guess the following is better/more correct:
Although the features are part of the standard library and not found in runtime,
p41 that is, A value => that is, a value
p42 Line 24: // string,string,string, => string,string,string,string
p45 reference contraction rule => I guess "reference collapsing rule" is a better name.
p53 except for std::pair There seems to be => there (Not capital T).
p55 , eliminating the display call delete, => , eliminating to call delete,
p57 Line 35: "p2 was destroied" => "p2 was destroyed"
p57 Line 40: destroied => destroyed
p58 Line 11: destroied => destroyed
p58 Line 18: destroied => destroyed
p58 no The way => no way
p59 Figure 2: Untranslated word, Chinese?
p66 std::lock_gurad => std::lock_guard
p66 RAII guarantees the exceptional security of the code while losing the simplicity of the code.
=> I guess here you are promoting RAII and the following is better:
RAII guarantees the exceptional security of the code while keeping the simplicity of the code.
p69 notd_one() of std ::condition_variable is used to wake up a thread =>
notify_one() of std ::condition_variable is used to wake up a thread
p71 Intuitively, a = 5;int2seems => Intuitively, a = 5; seems
Many thanks to Oguz Kupusoglu.
Was wondering what you would suggest
https://github.com/changkun/modern-cpp-tutorial/blob/master/book/zh-cn/07-thread.md
里面7.4的代码,
while (!notified) { // 循环避免虚假唤醒
cond_var.wait(lock);
}
notefied变量非volatile,是否存在这种问题:编译器优化时将其放置在寄存器中,线程更改变量时没有更新寄存器的变量,导致卡死
std::cout << "static_cast<T&&> 传参: ";
reference(std::forward<T>(v));
std::cout << "static_cast<T&&> 传参: ";
reference(static_cast<T&&>(v));
必要时,请附上相关截图
PDFs are large files that usually shouldn't be considered as a committed file to a git repository.
This repo initially committed all PDFs because they are reasonably small and light.
将亡值(xvalue, expiring value),是 C++11 为了引入右值引用而提出的概念(因此在传统 C++中,纯右值和右值是同一个概念),也就是即将被销毁、却能够被移动的值。
将亡值可能稍有些难以理解,我们来看这样的代码:
std::vector<int> foo() {
std::vector<int> temp = {1, 2, 3, 4};
return temp;
}
std::vector<int> v = foo();
原文解释如下
http://naipc.uchicago.edu/2015/ref/cppreference/en/cpp/language/value_category.html
A function call expression, if the function's return type is an rvalue reference to object type, such as std::move(val)
A cast expression to an rvalue reference to object type, such as static_cast<T&&>(val) or (T&&)val
a non-static class member access expression, in which the object expression is an xvalue
A pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.
将亡值可能稍有些难以理解,我们来看这样的代码:
std::vector<int> foo() {
std::vector<int> temp = {1, 2, 3, 4};
return std::move(temp);
}
std::vector<int> v = foo();
必要时,请附上相关截图
2.3 类型推导
“在介绍auto时,我们已经提过类型推导不能用于函数形参”
多写了一个“不”字,意思反了:(
看完了整本pdf,作为有C++98基础,新接触C++1z的新手,对于很多内容一开始都感到疑惑,自己另外查了很多资料才逐渐明白,建议作者把以下几点再丰富一下
C++11 提供了 constexpr 让用户显式的声明函数或对象构造函数在编译器会成为常量表达式...
“编译器” -> “编译期”
How to make to generate a single pdf not epub?
In decltype(auto) section, miss something?
std::string lookup1(std::string &str) {
return
}
std::string& lookup2();
...相比堆内存中的 std::vector,我们就能够灵活的访问这里面的元素,从而获得更高的性能;同时正是由于其堆内存存储的特性,有些时候我们还需要自己负责释放这些资源。https://github.com/changkun/modern-cpp-tutorial/blob/master/book/zh-cn/04-containers.md
我问过一些人,都是回答我释放资源是 std::vector 析构函数自动完成的,不需要手动释放。
请问什么时候需要手动释放?
我想了想,是说元素是指针的情况吗?
#include <iostream>
#include <string>
int main() {
std::string str = R"(C:\\What\\The\\Fxxk)";
std::cout << str << std::endl;
return 0;
}
int main() {
std::string str = R"(C:\What\The\Fxxk)";
std::cout << str << std::endl;
return 0;
}
必要时,请附上相关截图
Can you guys please support English translation because it would become famous and not everyone knows Chinese here.
Please briefly describe your motivation.
Please list all of your suggestions.
请描述你提交内容建议的动机。
请描述你提交内容建议的详单,例如具体是增加哪个知识点的说明。
第二章 语言可用性的强化
折叠表达式一节
template<typename ... T>
auto sum(T ... t) {
return (t + ...)
}
return语句缺少结束分号
3.2 函数对象包装器
函数和函数指针这一部分的表述不是非常准确,如下的解释感觉比较精确,是否可以参考
https://www.jianshu.com/p/6ecfd541ec04
std::weak_ptr 没有 * 运算符和 -> 运算符,所以不能够对资源进行操作,它的唯一作用就是用于检查 std::shared_ptr 是否存在,expired() 方法在资源未被释放时,会返回 true,否则返回 false
这里读起来的感觉更像是只有expired()方法,至少lock应该不只是有检查std::shared_ptr是否存在这一个作用
book/en-us/02-usability.md
A copy of the original paragraph
A modified paragraph
Attach screenshot or files if necessary.
3. 释放/获取模型:在此模型下,我们可以进一步加紧对不同线程间原子操作的顺序的限制,在释放 std::memory_order_release 和获取 std::memory_order_acquire 之间规定时序,即发生在释放操作之前的所有写操作,对其他线程的任何获取操作都是可见的,亦即发生顺序(happens-before)。
可以看到,std::memory_order_release 确保了它之后的写行为不会发生在释放操作之前,是一个向后的屏障,而 std::memory_order_acquire 确保了它之后的前的写行为,不会发生在该获取操作之后,是一个向前的屏障,对于选项 std::memory_order_acq_rel 而言,则结合了这两者的特点,唯一确定了一个内存屏障,使得当前线程对内存的读写不会被重排到此操作的前后。
而 std::memory_order_acquire 确保了它之后的前的写行为 此处描述错误
而 std::memory_order_acquire 确保了它之前的写行为
必要时,请附上相关截图
在某种意义上来说,传统 C++ 会把 NULL、0 视为同一种东西,这取决于编译器如何定义 NULL,有些编译器会将 NULL 定义为 ((void*)0),有些则会直接将其定义为 0。
且不论“编译器”定义 NULL
的说法来源是什么,“传统 C++”是指 ISO C++ 之前的(如由 Annotated Reference Manual 定义的) C++ 还是 ISO C++11 以前的 C++ ?指代不明。
注意 http://eel.is/c++draft/support.types.nullptr#footnote-187 是在什么时候引入的,烦请重新校对内容质量。
隐式转换到其他类型(换句话说,void * 并不属于 C++ 语言的一部分)
依据?
输出结果为:
传递右值:
普通传参:左值引用
std::move 传参:右值引用
std::forward 传参:右值引用
传递左值:
普通传参:左值引用
std::move 传参:右值引用
std::forward 传参:左值引用
无论传递参数为左值还是右值,普通传参都会将参数作为左值进行转发,所以 std::move 总会接受到一个左值,从而转发调用了reference(int&&) 输出右值引用。
std::move 总会接受到一个左值 ,但从上面的结果来看应该是右值吧?
#include <iostream>
using namespace std;
auto func1(auto a,auto b){
return a+b;
}
int main(){
cout<<func1(1,2)<<'\n';
return 0;
}
gcc 7.3 compile success.
需要支持以下内容:
constexpr
ifstatic_assert
inline
variablesconstexpr
改进, constexpr lambdaauto
non-type template parametersstd::string_view
, std::byte
加入 container 容器一章std::any
, std::variant
, std::optional
加入 container 容器一章std::shared_mutex
, atomic<T>::is_always_lockfree
, scoped_lock<Mutexes...>
new
book/en-us/02-usability.md
A copy of the original paragraph
A modified paragraph
Attach screenshot or files if necessary.
在 C++11 之后,编译器为我们做了一些工作,此处的左值 temp 会被进行此隐式右值转换,等价于 static_cast<std::vector &&>(temp),进而此处的 v 会将 foo 局部返回的值进行移动。也就是后面 我们将会提到的移动语义。
——————————————————————
这里并不会调用移动语义,而是由编译器直接进行了return value optimization。
只有你return std::move(temp)才会调用移动语义。
// 防止编译器优化
A return_rvalue(bool test) {
A a,b;
if(test) return a;
else return b;
}
a,b是左值,不是右值,上面的代码会调用拷贝构造函数,不是移动构造函数,如果
要掉用移动构造,需要调用std::move()将左值转成右值
// 防止编译器优化
A return_rvalue(bool test) {
A a,b;
if(test) return std::move(a);
else return std::move(b);
}
必要时,请附上相关截图
9.2 noexcept 修饰完一个函数之后能够起到封锁异常扩散的功效,如果内部产生异常,外部也不会触发。
我的理解是不会触发异常而执行catch中的代码,
如果我的理解正确的话:
之后给出的代码好像并不能体现出这一特性,
没有noexcept修饰符的my_throw()抛出了异常,但执行了catch中的代码
2,没有noexcept修饰符的non_block_throw()抛出了异常,也执行了catch中的代码
3,具有noexcept修饰符的block_throw()函数并没有抛出异常,当然也没有触发catch中的代码
4,于是我自做主张为my_throw() 增加了noexcept操作符使其声明变为
void may_throw() noexcept
这样的情况下:
4-1,编译器会发出警告( warning C4297: “may_throw”: 假定函数不引发异常,但确实发生了)
以及(note: 在函数上指定了 __declspec(nothrow)、throw()、noexcept(true)或 noexcept)(vs2017)
4-2,运行期间,程序直接异常退出。与我理解相反地,这个异常不能被try...catch语句捕捉,或许这才
你所要表达的“如果内部产生异常,外部也不会触发(try...catch语句)。”?
// 需要重新定义一个新的变量
const std::vector::iterator itr2 = std::find(vec.begin(), vec.end(), 3);
if (itr != vec.end()) {
*itr2 = 4;
}
The following code in 2.02.constexpr.cpp
:
int len = 10;
const int len_2 = len + 1;
char arr_4[len_2];
seems non-standard, because the const integral type len_2
can be treated as a constant expression only if it is initialized by a constant expression, but len + 1
is not a constant expression, so len_2
is NOT a constant expression (despite the fact that it is a const
variable), and thus it cannot be used to specify the size of an array. However, both gcc and clang allow arrays of variable length as an extension, but as for a tutorial for C++11/14/17 standard, this kind of behavior should not be encouraged. Also, if you add -pedantic
option to clang, a warning should be generated.
I really love to see more elaborations on the relationship between const
and constexpr
as it would easily confuse anyone new to modern C++. Personally I like the top answer in this question https://stackoverflow.com/questions/14116003/difference-between-constexpr-and-const, so it would be really nice if you could incorporate some of the content in the tutorial.
By the way, I've learned a lot from your tutorial. So thank you and keep up the good work!
decltype 关键字是为了解决 auto 关键字只能对变量进行类型推导的缺陷而出现的。它的用法和
sizeof 很相似
decltype 关键字是为了解决 auto 关键字只能对变量进行类型推导的缺陷而出现的。它的用法和
typeof 很相似
必要时,请附上相关截图
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.