-
Anti-Pattern: Enum as Array Index with No Bound Check
Recently, I encountered program crashes because of unscoped enum as array index pattern once in the workplace and another in an open source library where I added another item in the enum. Evidently, this anti-pattern is very popular among C++ developers. Open source code using Enum as Array Index pattern without bound check in its…
-
Put sleep() to make more money from HFT. And Lock-free vs Block-free
“Isn’t April Fools over?” the very first thought running through your mind after glancing at the title. Rest assured, this is not a trolling post to make a fool out of you. This year’s April Fool post is here, in case you missed the once-in-a-lifetime C++ joke of its kind. This article is about slowing…
-
C++23: -> and :: to be replaced by . operator
In the biggest change to C++ syntax, -> and :: operators are to be replaced by . operator in the upcoming C++23 AF00L proposal. The rationale behind this proposal is there can be no confusion (as to which operator to use). The :: operator is not only used in scope resolution and also as class-to-static_member.…
-
CreateCertificateContext Failed
CertCreateCertificateContext failure can be fixed with first exporting the certificate to DER Encoded Binary X.509 (cer file) manually. std::vector<BYTE> vec; PCCERT_CONTEXT pCertContext = NULL; if (pCertContext = ::CertCreateCertificateContext( PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, vec.data(), vec.size())) { std::cout << “Success!\n”; } else { std::cout << “Failure!\n”; } If one want to forgo the manual export option, CryptQueryObject can…
-
Comment Toggle Trick
This is a neat comment toggle trick. When you have two path of mutually exclusive code of execution, do this to enable the first section. Remove the first back slash to comment out the first section and uncomment the second section.
-
Avoid Default Clause in Switch Case for Enum
Do not define a default clause for enum in switch statement! Say in future, if another fruit type is added to the enum, default clause shall cause the C++ compiler not to warn you that you have not handled the case for the new fruit. enum class Fruit { Apple, Orange }; Fruit fruit =…
-
Private Inheritance
In private inheritance, the base class public access members are still public access in the derived class. Therefore the derived class still can call the base class functions and access its public data. But the user who instantiates the derived class, cannot access the public member of the base class, hence private inheritance. Public inheritance…
-
Digitally Signing Without Worrying About Cert Expiry
If you want your executable’s signature to be still valid after certificate validity period, you have to digitally sign it with a timestamp from one of the timestamp websites with /tr switch before its expiry. In my case, I choose timestamp.digicert.com. This command is for Windows only. signtool.exe sign /tr http://timestamp.digicert.com /f Cert.pfx /p password…
-
Avoid False Sharing With C++11 alignas Keyword
When two threads are frequently accessing and updating the 2 different but independent variables on the same cacheline, cache thrashing occurs as a result because when the thread updates its variable, the thread on another CPU core has its cacheline invalidated and have to fetch from the main memory again even though it is not…
-
WiX: Escape Square Blanket
In WiX, property is referenced by enclosing its name in square blanket. For example, “[foobar]”. When a text (which we have no control over) is enclosed in blankets but is not a property, how do we escape it? This is not a WiX problem but a MSI method of deducing whether a value is a…