https://habrahabr.ru/company/tensor/blog/326856/
https://habrahabr.ru/company/aligntechnology/blog/283352/ C++ without new and delete
http://ariasalpablo.blogspot.de/2017/05/understanding-virtual-tables-in-c.html
http://blog.rplasil.name/2016/10/crafting-c-continuation_21.html
https://github.com/caveofprogramming C++ dode from Udemy class
// Assignment: compare the two vectors for equality
// Note:Two instances should compare equal when the containing vaue is equal.
#include <vector>
#include <iostream>
struct A {
float value;
bool operator==(const A& a) const {
if (abs(this->value-a.value) < 0.0001f) return true;
return false;
}
bool operator !=(const A& a) const {
if (abs(this->value-a.value) < 0.0001f) return false;
return true;
}
};
int main() {
std::vector<A> v1 = {{1.f}, {2.f}, {3.f}, {4.f}};
std::vector<A> v2 = {{1.f}, {2.f}, {3.f}, {4.f}};
bool is_equal=(v1 == v2);
/*if (v1.size() != v2.size())
is_equal=false;
if (is_equal){
//float epsilon=0.00001;
for (unsigned int i=0; i<v1.size(); i++){
if (v1[i] != v2[i]){
is_equal=false;
break;
}
}*/
//}
std::cout << "Containers are " << (is_equal ? "equal" : "not equal") << "\n";
}
https://cdacamar.github.io/data%20structures/algorithms/benchmarking/mapping-strings/
http://pro-prof.com/forums/topic/cplusplus_pointers_references
http://www.acodersjourney.com/2016/06/the-quest-for-a-perfect-c-interview-question/
http://6uold.blogspot.com/2015/06/list-of-online-c-compilers.html
http://eli.thegreenplace.net/tag/multiple-dispatch
http://stackoverflow.com/questions/37361145/deoptimizing-a-c-program
http://www.agner.org/optimize/optimizing_cpp.pdf
http://articles.emptycrate.com/2016/04/18/stop_using_std_endl.html
http://ithare.com/no-bugs-top-five-cpp-cooking-recipes/
http://eli.thegreenplace.net/2016/returning-multiple-values-from-functions-in-c/
https://nikitablack.github.io/
https://cpp.zeef.com/michael.tkach
https://www.reddit.com/r/cpp/comments/4iwu1v/small_template_trick_getting_sizes_of_arbitrary/
http://tristanbrindle.com/posts/a-quicker-study-on-tokenising/
http://www.martin-ueding.de/en/programming/two-dimensional-array/index.html
https://news.ycombinator.com/item?id=11760946
http://www.bfilipek.com/2016/02/notes-on-c-sfinae.html
Fast code Alexandresku
https://www.youtube.com/watch?v=ph7FP0LnmcA
https://www.youtube.com/watch?v=3_FXy3cT5C8
http://cpptruths.blogspot.com/2015/11/covariance-and-contravariance-in-c.html
http://www.buildyourownlisp.com/
http://thispointer.com/handling-out-of-memory-errors-in-code/
http://jrruethe.github.io/blog/2015/08/23/placement-new/
http://nickdesaulniers.github.io/blog/2015/07/23/additional-c-slash-c-plus-plus-tooling/
http://www.gahcep.com/cpp-internals-memory-layout/
http://www.hwaci.com/sw/mkhdr/
http://www.memorymanagement.org/index.html
http://www.avabodh.com/cxxin/cxx.html C++ internals
do not use new and delete: https://www.youtube.com/watch?v=N0Tj2bDxQGo
http://www.reddit.com/r/cpp/comments/39gkm1/mikhail_matrosov_c_without_new_and_delete/
http://rohankshir.github.io/2015/05/15/choosing-the-right-datastructure/
http://habrahabr.ru/company/mailru/blog/267469/
https://turingtester.wordpress.com/2015/06/27/cs-rule-of-zero/
http://habrahabr.ru/company/xakep/
https://news.ycombinator.com/item?id=9674892
https://clip.mn/video/yt-1QhtXRMp3Hg defensive programming
http://habrahabr.ru/post/251091/
http://yapb-soc.blogspot.com/2012/12/quick-and-easy-manipulating-c.html
http://josephmansfield.uk/articles/exceptions-error-codes-assertions-c++.html
http://www.lonecpluspluscoder.com/2014/11/neater-way-handling-common-exceptions/
http://vickychijwani.me/cpp-gotchas/
http://habrahabr.ru/post/253749/
https://mentorembedded.github.io/cxx-abi/abi.html
http://habrahabr.ru/post/254621/ CGI server/client
http://channel9.msdn.com/Events/Build/2013/4-329 Native code performance
http://meta-x86.blogspot.co.il/2014/06/cpu-cache-essentials.html CPU cache
http://nadeausoftware.com/articles/2012/06/c_c_tip_how_loop_through_multi_dimensional_arrays_quickly
http://www.reddit.com/r/programming/comments/2nqrsr/cc_tip_how_to_loop_through_multidimensional/
http://www.bfilipek.com/2014/05/vector-of-objects-vs-vector-of-pointers.htm
http://habrahabr.ru/post/241231/ Persistent Queue
http://vickychijwani.me/cpp-gotchas/
http://quantumgraphics.blogspot.fr/2014/11/abusing-static-initialization.html
traits
http://thad.frogley.info/archive/cpp_traits_intro.html
http://accu.org/index.php/journals/442
TypeDef
http://thad.frogley.info/archive/using_typedefs.html
Virtual methods VTable explained
http://habrahabr.ru/company/pvs-studio/blog/239915/
http://aurisc4.blogspot.com/2015/04/c-inheritance-explained-part-ii.html
https://github.com/fffaraz/awesome-cpp
https://www.reddit.com/r/cpp/comments/3hrm78/monads_for_mortals_explains_the_monad_idiom_in_a/
Online Documentation
http://en.cppreference.com/w/Main_Page
https://github.com/L2Program/FlintPlusPlus LINT
http://www.reddit.com/r/cpp/comments/21pxx2/what_is_your_favorite_little_c_snippet_of_code/
http://habrahabr.ru/post/226333/ code organization
http://evgeny-lazin.blogspot.com/ blog
http://habrahabr.ru/post/207294/ type erasure
http://pfacka.binaryparadise.com/articles/guide-to-advanced-programming-in-C.html
http://blog.libtorrent.org/2013/12/memory-cache-optimizations/
http://www.bfilipek.com/2014/05/vector-of-objects-vs-vector-of-pointers.html
http://www.catb.org/esr/structure-packing/
https://techtalk.intersec.com/2014/02/more-about-locality/
http://codereview.stackexchange.com/
http://talesofcpp.fusionfenix.com/
http://blog.imperfectcplusplus.com/ Book: imperfect C++
http://www.semantics.org Book: C++ common knowledge
https://hex-wood.com/blog/?p=4 Kaspersky Code Oprimize
http://blog.aaronballman.com/tidbits/
http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL
http://habrahabr.ru/qa/48912/ calling C++ from C
http://habrahabr.ru/qa/46806/ sharing single struct between files
https://github.com/ryanhaining/cppitertools
http://www.codeproject.com/Articles/682455/Generic-Lazy-Evaluation-in-Cplusplus
http://habrahabr.ru/post/205316/ Object pool in C++
http://gameprogrammingpatterns.com/
The prefix increment operator changes an object's state and returns itself in the changed form. The prefix increment operator in the iterator class to handle std::vector may look this way:
_Myt& operator++()
{ // preincrement
return (*this);
++_Myptr;
}
The situation with the postfix increment is more complicated. The object's state must change but it is the previous state which is returned. An additional temporary object is created:
_Myt operator++(int)
_Myt _Tmp = *this;
{ // postincrement
++*this;
return (_Tmp);
}
If we want to just increment the iterator's value, it turns out that the prefix operator is more preferable. That is why, here you are one of the tips concerning software micro-optimization: write "for (it = a.begin(); it != a.end; ++it)" instead of "for (it = a.begin(); it != a.end; it++)". In the latter case, an unnecessary temporary object is created, which reduces performance.
assuming T::bar only throws something derived from std::exception:
void foo() { T* t = new T(); try { t->bar(); } catch(std::exception&) { delete t; throw; } delete t; }
Luckily std::unique_ptr solves all this, as if t->bar() throws then std::unique_ptr's destructor is called which calls delete t for us, hence both delete t lines are not needed and because we only catch an exception to throw it back, the try/catch block is not needed either thus reducing it to
void foo() { std::unique_ptr<T> t(new T()); t->bar(); }
and now we're protected against memory leaks.
http://habrahabr.ru/post/182588/
Reading Large Codebase
Working Effectively with Legacy Code. Michael Feathers (book)
http://www.spinellis.gr/codereading/ book
http://ayende.com/blog/164129/reading-large-codebases
http://www.reddit.com/r/programming/comments/1p6osf/reading_large_codebases/
http://www.sourceinsight.com/ (Windows only)
http://www.objectmentor.com/resources/articles/WorkingEffectivelyWithLegacyCode.pdf
http://www.slideshare.net/nashjain/working-effectively-with-legacy-code-presentation
Merging Sorted arrays in parallel
http://www.drdobbs.com/parallel/parallel-in-place-merge/240008783?queryText=stl
BIT Operations
http://www.stevenkitzes.com/2013/06/bitmask-basics.html
http://en.wikipedia.org/wiki/Mask_(computing)
http://www.drdobbs.com/parallel/a-conversation-with-bitmagics-developer/221900296?queryText=stl
http://bmagic.sourceforge.net/
int v = 0x12345678; for (int i = 31; i >= 0; i--) std::cout << ((v >> i) & 1);
There are many ways to do this; the loop above shifts the bit to print into the least significant bit (bit 0) of a temporary and ANDs it with 1 to remove all the other (higher order) bits and leave a 0 or 1. It starts with the most significant bit (31) and iterates down to the least significant bit (0), ensuring that the bits are printed in the correct order.
The good solution
While a looping solution still has applicability in some other languages, there is a much more elegant way to do it in C++ that is often overlooked:
int v = 0x12345678; std::cout << std::bitset<32>(v);
The C++ standard library includes a container type bitset whose first non-type template parameter specifies the number of bits to store. It includes an overload for operator<< and a conversion constructor for ints, making printing binary numbers a piece of cake! This is a much preferred solution to the messy for loop construct above.
To use bitset, remember to add:
#include <bitset>
Advanced C++
http://aszt.inf.elte.hu/~gsd/halado_cpp/
http://stackoverflow.com/questions/4421706/operator-overloading
http://vermeille.fr/dotclear2/index.php/
http://madebyevan.com/obscure-cpp-features/
CPP plugin framework
http://habrahabr.ru/post/164699/
http://www.drdobbs.com/cpp/building-your-own-plugin-framework-part/204202899
http://habrahabr.ru/post/198012/ using R from CPP
http://gameprogrammingpatterns.com/command.html Command Pattern
http://eli.thegreenplace.net/2013/10/17/getting-started-with-libjit-part-1/ Just in Time compiler
C++ from python
http://www-h.eng.cam.ac.uk/help/languages/python/python_and_C.html
https://intermediate-and-advanced-software-carpentry.readthedocs.org/en/latest/c++-wrapping.html
Unordered map
http://bannalia.blogspot.com/2013/10/implementation-of-c-unordered.html
http://www.drdobbs.com/windows/user-defined-hash-functions-for-unordere/231600210#
http://www.drdobbs.com/cpp/auto-types-and-range-based-for-statement/232900460#
http://www.drdobbs.com/cpp/c-stl-hash-containers-and-performance/198800559#
http://users.livejournal.com/_winnie/241837.html map.find
http://www.kumobius.com/2013/08/c-string-to-int/ STRING TO INT
GCC
http://habrahabr.ru/post/151314/
http://www.antoarts.com/the-most-useful-gcc-options-and-extensions/
http://www.developingthefuture.net/difference-between-generics-in-c-java-and-c/
Chromium Project
https://www.mobilespan.com/content/chrome-is-the-new-c-runtime
C++ code in /base subfolder of Chromium is best C++ code for learning
http://www.chromium.org/developers/how-tos/getting-around-the-chrome-source-code
Eclipse C++
http://wiki.eclipse.org/CDT/User/FAQ
http://www.drdobbs.com/cpp/cross-platform-cc-development-with-eclip/232300575
article explores how to create one Eclipse project that can be used to compile on Windows using the Visual Studio compiler or on Linux using GCC, and share the source code via SVN.
I'm running 64-bit Eclipse Indigo with the plugins CDT, Subclipse, and SVNKit (included with Subclipse).
http://www.eclipse.org/forums/index.php/t/490066/
https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Developer_Guide/
http://www.eclipsecon.org/2013/sessions/profiling-cc-applications-using-eclipse
http://xmodulo.com/2013/08/how-to-set-up-c-cpp-development-environment-in-eclipse.html
http://tutorialforlinux.com/2012/10/16/eclipsecpphelloworld/
assert(ptr != NULL && "Invalid pointer");
free(NULL) is defined as a no-op by the standard. Every time you see an if guard on a free call, it can be removed
http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer
We can say "pass by pointer" is passing a pointer by value. In most cases, this does not present a problem. But problem comes when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified, that is, it still points to the old variable.
Function pointers
http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
http://vickychijwani.me/function-pointers-in-c-are-underrated/
http://rsdn.ru/article/mag/200406/fastdelegate.xml
http://en.wikipedia.org/wiki/Function_pointer
http://www.cs.wustl.edu/~schmidt/PDF/C++-ptmf4.pdf
http://yosefk.com/c++fqa/function.html
http://www.crawfordology.net/tips/code/c++/member-pointers.html
C++ metaprogramming book
Smart pointers:
http://habrahabr.ru/post/140222/
https://news.ycombinator.com/item?id=11698784
http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/
http://herbsutter.com/gotw/_103/
https://www.youtube.com/watch?v=bxaj_0o4XAI
http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/
http://dlib.net/ Scientific/Machine Learning/XML libraries
IDIOMS
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms
http://www.slideshare.net/andreialexandrescu1/three-optimization-tips-for-c-15708507
PIMPL
https://habrahabr.ru/post/311038/
http://bitsquid.blogspot.it/2012/03/pimpl-vs-pure-virtual-interfaces.html PIMPL
C++: How do I pass a container of derived classes to a function expecting a container of their base classes?
Answer: you can't. Child pointers are convertible to base pointers, containers of child pointers are not.
This is not possible in C++, this requires a feature called covariance.
Even if type A is a subclass of type B, type X<A> is completely unrelated to type X<B>
Thus you cannot pass std::vector<ChildClass> to a function expecting std::vector<ParentClass>,
since they are unrelated types. Even pass by reference/pointer will not work.
There are two ways to get around this.
1) make both vectors hold pointers to ParentClass, then they would have identical types.
2) make the function a template function:
template< typename T >
getUniqueLabels(std::vector<T> events);
http://www.linuxforu.com/2011/08/lets-hook-a-library-function/ dlsym
http://www.techgineering.org/2011/09/13/2278/top-7-online-compilers-and-ides/
POD (for Plain Old Data) — that is a struct or class without constructors, destructors and virtual members functions. __is_pod http://habrahabr.ru/post/150970/
https://github.com/quarksbar/tcpppl_answers
SINGLETON
Copy constructor must be pass by reference, because pass-by-value would invoke the copy constructor :)
http://www.cplusplus.com/articles/jsmith1
If you need one of: destructor, copy c'tor or copy assign, then you probably need all three. The exception here is if you wish to deliberately disable copying by making the copy c'tor and copy assign private (and unimplemented) - you won't always need to define your own d'tor in this case
How to disable copy constructor and assignment operator:
http://www.cplusplus.com/reference/clibrary/ctime/difftime/ measuring time
http://umumble.com/blogs/cpp/34/#more overflow in multiplication
HEADER FILES ORGANIZATION
http://code.google.com/p/include-what-you-use/
http://freecode.com/projects/deheader
http://akhodakivskiy.github.com/2012/12/11/private-implementation-forward-declaration.html
http://bitsquid.blogspot.it/2012/09/a-new-way-of-organizing-header-files.html
http://blog.kowalczyk.info/article/qg/Order-of-include-headers-in-CC.html
http://blog.knatten.org/2012/11/30/how-to-avoid-includes-in-headers/
Callbacks
http://bitsquid.blogspot.it/2011/02/managing-decoupling-part-2-polling.html
http://www.linuxforu.com/2012/02/function-pointers-and-callbacks-in-c-an-odyssey/
Volatile
http://habrahabr.ru/company/abbyy/blog/161607/
Serialization
http://bitsquid.blogspot.it/2009/10/two-way-serialization-function.html
https://github.com/burrows-labs/templates/tree/master/serial
https://github.com/ipkn/dumpable
Error Handling
http://cppsecrets.blogspot.ca/2013/12/using-lippincott-function-for.html
http://www.reddit.com/r/programming/comments/1ss9j2/c_secrets_using_a_lippincott_function_for/
http://www.yosefk.com/blog/error-codes-vs-exceptions-critical-code-vs-typical-code.html
http://experience.openquality.ru/exception-handling-2/
http://bitsquid.blogspot.it/2012/01/sensible-error-handling-part-1.html
http://bitsquid.blogspot.it/2012/02/sensible-error-handling-part-2.html
http://bitsquid.blogspot.it/2012/02/sensible-error-handling-part-3.html
http://exceptionsafecode.com/
NSErrors from Cocoa. They encapsulate all the relevant information about an error:
- Domain ("POSIX", "CFNetwork", "AudioQueue") as a string
- Error code (-129, EADDRINUSE, 42)
Memory allocation
https://rawgit.com/google/cxx-std-draft/allocator-paper/allocator_user_guide.html
http://bulldozer00.com/2015/09/14/stack-heap-pool/
http://danluu.com/malloc-tutorial/
http://habrahabr.ru/post/244617/
http://habrahabr.ru/post/158347/
http://habrahabr.ru/post/148657/
http://habrahabr.ru/post/148242/
http://habrahabr.ru/post/125435/
http://habrahabr.ru/post/162187/
http://nethack4.org/blog/memory.html
http://bitsquid.blogspot.se/2010/09/custom-memory-allocation-in-c.html
http://www.codeproject.com/Articles/447975/Placement-new-and-performance-in-Cplusplus
http://happybearsoftware.com/implementing-a-dynamic-array.html
http://jfdube.wordpress.com/2011/10/22/memory-management-part-3-memory-allocators-design/
http://stackoverflow.com/questions/6500313/in-c-why-should-new-be-used-as-little-as-possible
http://linuxdevcenter.com/lpt/a/4046 C++ Memory Management: From Fear to Triumph
http://bryanstamour.com/2012/06/26/need-polymorphism/
STL custom allocator
http://en.wikipedia.org/wiki/Allocator_(C%2B%2B)
http://www.cplusplus.com/reference/std/memory/allocator/
http://stackoverflow.com/questions/4642671/c-memory-allocators
http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement
http://jrepp.com/2007/03/21/writing-a-replacement-stl-allocator/
http://www.sjbrown.co.uk/2004/05/01/pooled-allocators-for-the-stl/
http://devnikhilk.blogspot.com/2011/08/sample-stl-allocator.html
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079/Allocators-STL.htm
http://www.drdobbs.com/cpp/improving-performance-with-custom-pool-a/184406243
http://www.drdobbs.com/creating-stl-containers-in-shared-memory/184401639
http://bitsquid.blogspot.se/2010/09/custom-memory-allocation-in-c.html
http://blogs.msdn.com/b/calvin_hsia/archive/2010/03/16/9979985.aspx
http://stackoverflow.com/questions/8247552/replace-default-stl-allocator
http://www.thelittlebalance.com/?p=430
http://bitsquid.blogspot.it/2010/09/custom-memory-allocation-in-c.html
Garbage collection
http://habrahabr.ru/search/?q=Garbage+collection
http://www.rsdn.ru/article/cpp/GCcpp.xml
http://www.linuxprogrammingblog.com/all-about-linux-signals
http://blog.incubaid.com/2012/04/02/tracking-asynchronous-io-using-type-systems/
Memory leak:
http://cuppadev.co.uk/help-i-have-a-memory-leak/
http://www.codeproject.com/Articles/393957/Cplusplus-Memory-Leak-Finder
Microsoft Application Verifier
http://randomascii.wordpress.com/2011/12/07/increased-reliability-through-more-crashes/
http://randomascii.wordpress.com/2011/10/15/try-analyze-for-free/
Generating core dump http://honglus.blogspot.com/2010/06/how-to-generate-thread-dump-for.html
Define Template in Header File
http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp
http://isocpp.org/blog/2014/09/templates-series
Expression Template
http://en.wikipedia.org/wiki/Expression_templates
http://www.bnikolic.co.uk/blog/cpp-expression-minimal.html
http://www.bnikolic.co.uk/lazy/intropaper.html
The 2011 standard of C++ includes rvalue references, which remove the need for expression templates in the most common cases. An alternative technique is to directly emulate move semantics, for example using the Boost move library
http://en.wikipedia.org/wiki/Return_value_optimization
http://shoddykid.blogspot.com/2010/11/copy-your-temporaries-return-by-value.html
http://shoddykid.blogspot.com/2008/08/expression-templates-demystified-part-2.html
http://www.bnikolic.co.uk/blog/cpp-expression-minimal.html
http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Expression-template
http://alpmestan.wordpress.com/2009/11/01/why-expression-templates-matter/
http://arxiv.org/abs/1109.1264
http://www.mrao.cam.ac.uk/~bn204/publications/2011/boostcon2011slides.pdf
http://sourceforge.net/projects/exmat/
http://www.sci.utah.edu/~wolters/PaperWolters/HaerdtleinEtAl_CompVisSci_2010.pdf
http://www.parashift.com/c++-faq-lite/virtual-functions.html
http://www.viva64.com/en/l/full/print/ 64 bits software development
http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-1-starting-threads.html multithreading
Searching inside source code: ack(Perl) pss (python)
http://eli.thegreenplace.net/2011/10/14/announcing-pss-a-tool-for-searching-inside-source-code/
RTTI
http://en.wikipedia.org/wiki/RTTI
http://www.rsdn.ru/article/cpp/static_cast.xml
http://www.cplusplus.com/doc/tutorial/typecasting/
http://www.java2s.com/Tutorial/Cpp/0220__Pointer/Usedynamiccasttoreplacetypeid.htm
Reflection, RAII
http://en.wikipedia.org/wiki/RAII
http://habrahabr.ru/company/pt/blog/255487/ RAII
http://habrahabr.ru/post/207842/ RAII smart pointer
SFINAE
http://codeofthedamned.com/index.php/sfinae
http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
http://habrahabr.ru/post/205772/ SFINAE
http://debugfailure.wordpress.com/2009/10/06/understanding-sfinae/
http://habrahabr.ru/post/150654/
http://altdevblogaday.com/2011/09/25/reflection-in-c-part-1-introduction/
https://habrahabr.ru/post/277329/
Comparing float numbers
http://altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/
Writing libraries
http://habrahabr.ru/blogs/cpp/132300/
http://habrahabr.ru/blogs/cpp/138438/
Polimorphism. Inharitance
http://www.alexonlinux.com/how-inheritance-encapsulation-and-polymorphism-work-in-cpp
http://mortoray.com/2011/08/12/how_polymorphism_works-part2-virtual-table/
Templates
http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1
http://www.codeproject.com/Articles/268849/An-Idiots-Guide-to-Cplusplus-Templates-Part-2
http://www.cplusplus.com/doc/tutorial/templates/
http://itee.uq.edu.au/~conrad/misc/sanderson_templates_lecture_uqcomp7305.pdf
http://www.cs.tut.fi/~kk/webstuff/MetaprogrammingCpp.pdf
http://eli.thegreenplace.net/2011/04/22/c-template-syntax-patterns/
http://www.developingthefuture.net/difference-between-generics-in-c-java-and-c/
http://www.berti-cmm.de/en/techinfo/generic-programming/tutorial/
template <typename T>
T Max(T lhs, T rhs) {
return (lhs > rhs) ? lhs : rhs;
}
Usage:
int max = Max<int>(100, 200);
int max = Max(100, 200);
Performance:
http://www.agner.org/optimize/
http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Writing_efficient_code
http://stackoverflow.com/questions/8547778/why-is-one-loop-so-much-slower-than-two-loops
http://www.tophatstuff.co.uk/?p=119
http://habrahabr.ru/blogs/programming/124910/#habracut
OOP
The key feature of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this feature.
Abstract base class has abstract method (pure virtual function):
virtual int area () = 0; // must be overidden in child class
Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object.
In contrast, virtual member functions are resolved dynamically (at run-time). That is, the member function is selected dynamically (at run-time) based on the type of the object, not the type of the pointer/reference to that object. This is called "dynamic binding." Most compilers use some variant of the following technique: if the object has one or more virtual functions, the compiler puts a hidden pointer in the object called a "virtual-pointer" or "v-pointer." This v-pointer points to a global table called the "virtual-table" or "v-table."
The compiler creates a v-table for each class that has at least one virtual function.
Constructor / destructor
The construction order of the members is the order in which they are defined, and for this reason the same order should be preserved in the initialization list to avoid confusion.
To build an object the constructor must be of the same type as the object and because of this a constructor cannot be a virtual function. But the same thing does not apply to destructors. A destructor can be defined as virtual or even pure virtual. You would use a virtual destructor if you ever expect a derived class to be destroyed through a pointer to the base class. This will ensure that the destructor of the most derived classes will get called:
Scott Meyers, More effective C++, item 27.
If you want to force the creation of an object on the heap,then you use a private destructor and provide a public
member function destroy():
void destroy() const
{delete this;}
Stack only class:
class A{
private:
void * operator new(size_t size) {}
};
vectorization intrinsic SSE4
http://en.wikipedia.org/wiki/Intrinsic_function
http://stackoverflow.com/questions/661338/sse-sse2-and-sse3-for-gnu-c
http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
http://habrahabr.ru/company/intel/blog/131159/
http://www.thingsstuff.com/2011/12/05/writing-maintainable-simd-intrinsics-using-c-templates/
Static code analyzer
http://cppcheck.sourceforge.net/
http://www.viva64.com/ru/pvs-studio/
http://altdevblogaday.com/2011/12/24/static-code-analysis/
http://msdn.microsoft.com/en-us/library/d3bbz7tz%28v=VS.100%29.aspx
http://habrahabr.ru/blogs/code_review/135234/
http://www.reddit.com/r/programming/comments/nov8m/john_carmack_on_static_code_analysis/
http://www.campwoodsw.com/sourcemonitor.html
http://msdn.microsoft.com/en-us/windows/hardware/gg487345.aspx PreFast
http://stackoverflow.com/questions/97454/c-static-code-analysis-tool-on-windows
http://www.stack.nl/~dimitri/doxygen/config.html#cfg_call_graph
http://stackoverflow.com/questions/141498/what-open-source-c-static-analysis-tools-are-available
http://stackoverflow.com/questions/27857/c-c-source-code-visualization
http://daniel-wilkerson.appspot.com/oink/index.html
Operator << overloading
#include <iostream>
class Point{
public:
Point(int x, int y) : x_(x), y_(y) { }
std::ostream& write(std::ostream& os){ return os << "[" << x_ << ", " << y << "]"; }
private:
int x_, y_;
}; //end of class Point definition
std::ostream& operator<< (std::ostream& os, const Point& point) { return point.write(os); }
int main() {
Point point(20, 30);
std::cout << "point = " << point << "\n";
}
Assignment and copy constructor
Assignment and copy constructor are different because
- the copy constructor initializes uninitialized memory,
- but assignment starts with an existing initialized object.
class A {
public:
A ( const A&); // copy constructor
A& operator=(const A&); //assignment ; if implemented should return *this
private:
int i;
};
A a1, a2; //default ctor
A a3(a2); //copy ctor
A a2=a1; //calls copy ctor
a3=a2; //calls assignment ctor
http://www.devexp.ru/2010/03/o-kopirovanii-obektov-v-c/#more-72
A function object (or functor)
http://habrahabr.ru/post/234215/
http://www.drdobbs.com/cpp/efficient-use-of-lambda-expressions-and/232500059
std::count_if(v.begin(), v.end(), some_function(n));
is simply any object of a class that provides a definition for operator() What this means is that if you then declare an object f of the class in which this operator() is defined you can subsequently use that object f just like you would use an "ordinary" function. For example, you could have an assignment statement like
someValue = f(arg1, arg2); which is the same as
someValue = f.operator()(arg1, arg2);
struct reverseSort
{
bool operator()(int a, int b) { return b < a; }
};
//sort the elements using custom functor
sort(v.begin(), v.end(), reverseSort());
http://www.newty.de/fpt/index.html
#include <functional>
Arithmetic binary functors
plus<T> f; f(arg1, arg2) returns the value arg1 + arg2.
minus<T> f; f(arg1, arg2) returns the value arg1 - arg2.
multiplies<T> f; f(arg1, arg2) returns the value arg1 * arg2.
divides<T> f; f(arg1, arg2) returns the value arg1 / arg2.
modulus<T> f;f(arg1, arg2) returns the value arg1 % arg2.
Relational binary functors
equal_to<T> f; f(arg1, arg2) returns the value arg1 == arg2.
not_equal_to<T> f; f(arg1, arg2) returns the value arg1 != arg2.
greater<T> f; f(arg1, arg2) returns the value arg1 > arg2.
greater_equal<T> f; f(arg1, arg2) returns the value arg1 >= arg2.
less<T> f; f(arg1, arg2) returns the value arg1 < arg2.
less_equal<T> f; f(arg1, arg2) returns the value arg1 <= arg2.
Logical binary functors
logical_and<T> f; f(arg1, arg2) returns the value arg1 && arg2.
logical_or<T> f; f(arg1, arg2) returns the value arg1 || arg2.
Built-in functors of unary type
Arithmetic unary functor
negate<T> f; f(arg) returns the value -arg.
Logical unary functor
logical_not<T> f; f(arg) returns the value !arg
vector <int> v1;
vector <int> v2;
transform (v1.begin (), v1.end (), v2.begin (), negate);
std::bind2nd
http://www.sgi.com/tech/stl/functors.html
http://www.keithschwarz.com/cs106l/spring2009/handouts/280_STL_Functional_Library.pdf
http://waqqasfarooq.com/waqqasfarooq/index.php?option=com_content&view=article&id=61&Itemid=70
FOR_EACH
void call_me(const std::string &s){ std::cout << s << std::endl;}
void work(){
std::vector<std::string> v;
std::for_each(v.begin(), v.end(), &call_me); //do we need & here
}
http://xenon.arcticus.com/c-morsels-std-for-each-functors-member-variables
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f);
Effects: Applies f to the result of dereferencing every iterator in the range [first, last) , starting from first and proceeding to last - 1 .
Returns: f
Complexity: Applies f exactly last - first times.
Notes:If f returns a result, the result is ignored.
stl::not1
http://blog-of-darius.blogspot.com/2009/04/c-stl-findif-and-not1-used-to-fined.html
http://stackoverflow.com/questions/4583310/how-to-negate-a-predicate-function-using-operator-in-c
STL::SORT
http://www.codeproject.com/Articles/38381/STL-Sort-Comparison-Function
TRANSFORM
It comes in two flavors, one version (unary transform) works on one input sequence, the other version (binary transform) takes two input sequences. Since we want to compare for_each and transform , we only consider the unary transform:
template<class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform(InputIterator first, InputIterator last,
OutputIterator result, UnaryOperation op);
Effects: Assigns through every iterator i in the range [result, result + (last - first)) a new corresponding value equal to op(*(first + (i - result)) .
Requires: op shall not have any side effects.
Returns: result + (last - first)
Complexity: Exactly last - first applications of op.
Notes: result may be equal to first.
The unary transform algorithm and the for_each algorithm both apply a unary operation to every element in a range of input iterators exactly once. Other than that they have little in common. The differences include:
for_each is a non-modifying algorithm; transform is a mutating algorithm.
for_each ignores the operation's return value; transform assigns the return value to successive elements in the output range.
for_each returns a copy of the function object; transform returns an iterator to the end of the output range.
for_each applies the operation in a definite order, namely starting at the beginning and proceeding to the end of the input range; no such guarantee is given for transform .
The operation supplied to transform must not have any side effects; no such restriction is imposed on the operation supplied to for_each.
http://www.angelikalanger.com/Articles/Cuj/03.ForeachTransform/ForEachTransform.html
For each word in file, return # of occurrences:
http://bannalia.blogspot.com/2008/05/word-frequency-and-length.html
#include <cstdio> // easier than iostream for formatted output
#include <iostream>
#include <iterator>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>
typedef std::map<std::string, std::size_t> WordCountMapType;
WordCountMapType wordsInFile(const char * const fileName) {
std::ifstream file(fileName);
WordCountMapType wordCounts;
for (std::string word; file >> word; ) {
++wordCounts[word];
}
return wordCounts;
}
Modern C++ code
http://en.highscore.de/cpp/boost/frontpage.html BOOST
http://www.reddit.com/r/cpp/comments/wr68y/modern_source_code_to_study/
POCCO http://habrahabr.ru/post/148173/ http://habrahabr.ru/post/148227/
U++ http://www.ultimatepp.org/
Doxygen
http://www.stack.nl/~dimitri/doxygen/config.html#cfg_call_graph
http://habrahabr.ru/post/252101/
Use doxywizard.exe GUI: switch to EXPERT mode
under the Topic "Input" specify INPUT folder
To generate SVG diagrams install Graphviz; under the Topic "Dot" assign the DOT_PATH to Graphviz folder
http://eigen.tuxfamily.org/ Free Matrix Library
https://github.com/ForceBru/Matrix/tree/master/Matrix
Read a file into a vector of strings
vector<string> V;
copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(V));
http://www.keithschwarz.com/cs106l/spring2009/handouts/280_STL_Functional_Library.pdf
http://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c
http://www.mochima.com/tutorials/STL_algorithms.html#other_operations