C++ syntax highlighting for copy+paste html content
// **test_map.cpp **********************************************
// g++ test_map.cpp -static -o test_map #include <map>#include <iostream> struct Point { Point(int theX, int theY) : x(theX), y(theY) {} int x; int y; bool operator <(const Point& rhs) const { return x < rhs.x && y < rhs.y; }};int main(){ std::map<Point, int> m; m[Point(0,1)] = 1; m[Point(1,0)] = 2; std::cout << m[Point(0,1)] << "\n"; std::cout << m[Point(1,0)] << "\n";}// ******************************************************
//**test_template.cpp*************************************// For more info: please view at “B.13.6 template” in // Special Edition, Biern Stroustrup
// Текст из главы: Необходимость в квалификаторе template необходима в случае затруднения понимания
// того к чему надо выводить template <typename S, typename T> T* allocate(S& storage, int numberOfElements){ T* res = 0;// res = storage.alloc<T>(numberOfElements); res = storage.template alloc<T>(numberOfElements); return res ? true : false;} int main(int argc, char *argv[]){ return 0;}//*******************************************************
//***specialization_in_class.cpp ****************************
template<class T>class ScopeClass { template<class B> struct A { B var; };// NOT WORK A /* template<> struct A<int> { int var; int extra; };*/};// NOT WORK B /* template<class T> template<> struct ScopeClass<T>::A<int>{ int var; int extra;};*/// WORKED C template<> template<> struct ScopeClass<int>::A<int>{ int var; int extra;};int main(){return 0;}// Answer:// http://stackoverflow.com/questions/3052579/explicit-specialization-in-non-namespace-scope/3052604#3052604 // Georg Fritzsche answer //************************************************************************************************************
// ctor_for_virtual_base_1st_step_in_ctor_logic.cpp
#include <stdio.h>class A {public: A() {printf("A()\n");}};class B:virtual public A {public: B() {printf("B()\n");}};class C: /*virtual*/ virtual public A {public: C(){printf("C()\n");}};class Branch {public: Branch(){printf("Branch()\n");}};class D : public Branch, public B, virtual public C {public: D(){printf("D()\n");}};int main(){ D d; return 0;}
https://isocpp.org/wiki/faq/inline-functions
Как нельзя работать с inline функциями
// inline.cpp#include <stdio.h>inline void f(){ printf("F inline");}// inline.hinline void f();// main.cpp#include "inline.h"int main(){ f(); return 0;}
=>
unresolved external symbol "void __cdecl f(void)" (?f@@YAXXZ) referenced in function _main
============================================
Как надо работать с inline функциями
// inline.cpp// empty// inline.h#include <stdio.h>void f();inline void f(){ printf("F inline");}// main.cpp#include "inline.h"int main(){ f();
return 0;
}
// Трюк как задекларировать специализацию шаблона для массива
#include <stdio.h>template<class T>void p(T a){ puts("not array"); }template<class T, int sz>void p( T (&t)[sz] ) { puts("array"); }int main(int argc, char *argv[]){ int q[3]; p(q); return 0;}