stevenhalim / cpbook-code

CP4 Free Source Code Project (C++17, Java11, Python3 and OCaml)
2k stars 493 forks source link

Illegal C++ identifiers #66

Closed howsiwei closed 4 years ago

howsiwei commented 4 years ago

This repository has a few C++ code that uses variable with name that begins with an underscore followed by an uppercase letter. However, this is technically undefined behavior in C++.

According to https://en.cppreference.com/w/cpp/language/identifiers,

the identifiers that begin with an underscore followed by an uppercase letter are reserved;

"Reserved" here means that the standard library headers #define or declare such identifiers for their internal needs, the compiler may predefine non-standard identifiers of that kind, and that name mangling algorithm may assume that some of these identifiers are not in use. If the programmer uses such identifiers, the behavior is undefined.

stevenhalim commented 4 years ago

Noted, which cpp files got that? I remember using a few like this but it shouldn't be that many and can be fixed easily.

stevenhalim commented 4 years ago

I spotted SuffixArray _T max_flow _V so far will fix them

the _x and _y for points (ch7) should be ok I guess

howsiwei commented 4 years ago
$ rg -tcpp '\b_[A-Z]'
ch8/maxflow.cpp
68:  max_flow(int _V) : V(_V) {

ch2/ourown/segmenttree_ds.cpp
71:  SegmentTree(const vi &_A) : SegmentTree((int)_A.size()) {
72:    A = _A;
152://   SegmentTree(const vi &_A) : SegmentTree((int)_A.size()) {
153://     A = _A;

ch6/sa_lcp.cpp
71:  SuffixArray(const char* _T, const int _n) : T(_T), n(_n) {

ch9/SparseTable.cpp
13:  SparseTable(vi &_A) : A(_A) {                  // pre-processing routine
36:        SpT[i][j] = _A[x] <= _A[y] ? x : y;

ch9/mcmf.cpp
68:  min_cost_max_flow(int _V) : V(_V), total_cost(0) {
stevenhalim commented 4 years ago

Thanks, all fixed on my side and in the body text, will push these updates in a few minutes.

Apparently this _ thingy only appear in class construction when I have same variable name between internal variable and the parameter of the constructor ...