Category Archives: C++ Language

division & modulus

a / b, a % b 當b為0時, behavior undefined,這邊討論其中一個operand為負數的情況 C89 整數除法(有負數情況下 implementation defined) -9/7 有可能是 -1 or -2 (rounded up or rounded down) 當然此時對應的 % 就會根據除法的結果而不同 (這邊討論整數的modulus,floating point可以參考fmod) 要符合 dividend = divisor * quotient + remainder -9 = 7 * … Continue reading

Posted in C Language, C++ Language | Leave a comment

header only files tips

當開發header only library時,有時候會需要static or global物件,但因為C++ ODR的原因, 當物件被重複定義時在link階段會報錯。 例如 //X.h class X { public: static int m; //這只是declaration }; //main.cpp #include “X.h” int main() { X a; a.m = 0; return 0; } /tmp/ccaFpbvF.o: In function `main’: t.cc:(.text+0x6): undefined reference to … Continue reading

Posted in C++ Language | Leave a comment

C++ exit abort與destructor

#include #include class X { int i; public: X(int i): i(i) { } ~X() { std::cout

Posted in C++ Language | Leave a comment

shared_ptr + lambda

在C++11裡新支援了lambda, 這也使得編寫C++程式可以多了一些FP的風格。 lambda特別是在處理callback時特別簡潔,因為通常callback function是為了特定api打造的,單獨定義會讓程式碼比較分散,在閱讀上也常會被打斷。 在沒有capture情況下,lambda可以直接當成ordinary function pointer lambda如果有capture variable, 則可以想像成compiler透過functor的技巧打包,此時不能用在使用function pointer的callback #include typedef void (*callback)(int); void test(callback cb) { cb(100); } int main() { //capture-less lambda, can be treated like function pointer test([](int n){ std::cout

Posted in C++ Language | Leave a comment

int() = 0

顯式的使用 int() 會回傳0int a = int(); //a=0這個在語法上看起來「像」是constructor,而效果上也很「像」constructor (default value = 0) 那麼這個語法的值是怎麼被決定的呢?參考c++03 5.2.3 Explicit type conversion (functional notation) 的第二點 The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, … Continue reading

Posted in C++ Language | Leave a comment

protected destructor

constructor access modifier 一般設為public,而在singleton中,為了限制生成的物件個數,會將constructor放在private,另外在其他method生成物件。而對於destructor,以Meyers Singleton的實作來說,destructor放在public or non-public皆可,因為static object 在宣告的地方可以存取到private destructor(參考下面12.4) C++03 std §3.6.3 Termination Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main … Continue reading

Posted in C++ Language | Leave a comment

std::make_shared

//C++11中的 shared_ptr初始化一般使用 std::shared_ptr p(new A); //但也可以 std::shared_ptr p = std::make_shared(); 使用make_shared可以避免顯式地使用new,另外主要的差別在make_shared 一般會將shared_ptr內的control block和object一起allocate。雖然一次allocate上比較有效率,但如果有用到weak_ptr,當shared count = 0時,不會馬上還回記憶體,因為control block還需要用到,而是要等到沒有任何shared_ptr, weak_ptr參考時,才會釋放記憶體。 在N3690文件 20.9.2.2.6 shared_ptr creation 中提到: Remarks: Implementations should perform no more than one memory allocation. [ Note: This provides efficiency equivalent to … Continue reading

Posted in C++ Language | Leave a comment

string::assign

#include #include int main() { std::string x(“0123456789”); x.assign(x.data() + 3, 5); std::cout size(), __n, “basic_string::assign”); if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) return _M_replace_safe(size_type(0), this->size(), __s, __n); else { // Work in-place. const size_type __pos = __s – _M_data(); if (__pos >= __n) … Continue reading

Posted in C++ Language | Leave a comment

return void type

在C++ void function()中,如果要提前返回的話,只要加上return;那麼,是否可以return 一個void function呢? 答案是可以的,以下是C++03標準的摘錄 A return statement with an expression of type “cv void” can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its … Continue reading

Posted in C++ Language | Leave a comment