Wednesday, December 30, 2009

C++98: NTCTS

In the C++ standard, NTCTS stands for "null-terminated character-type sequence".
Reference: C++ Common Knowledge by Stephen C. Dewhurst. Addison-Wesley, 2005., p. 205.

Monday, December 28, 2009

C++98: Class Templates

Class templates must be specialized explicitly.
Reference: C++ Common Knowledge by Stephen C. Dewhurst. Addison-Wesley, 2005., p. 209.

Saturday, December 26, 2009

C++98: Cast Operators

Although, static_cast, dynamic_cast, reinterpret_cast, and const_cast all look like templates; they are not. They are operators.
Reference: C++ Common Knowledge by Stephen C. Dewhurst. Addison-Wesley, 2005., p. 210.

Thursday, December 24, 2009

C++98: Overloaded Operators

An overloaded operator has the same precedence as the original operator.
Reference: C++ Strategies and Tactics by Robert B. Murray. Addison-Wesley, 1993., p. 33.

Tuesday, December 22, 2009

C++98: Class Member Operators

If the following operators are overloaded, they must be class members: =, [], (), ->
Reference: C++ Strategies and Tactics by Robert B. Murray, Addison-Wesley, 1993., p. 46.

Sunday, December 20, 2009

C++98: Copy Constructors

A copy constructor of the following form is illegal in C++: A::A(A).The argument should be const A &.
Reference: C++ Strategies and Tactics by Robert B. Murray, Addison-Wesley, 1993., p. 60.

Friday, December 18, 2009

C++98: Nested Classes

Nested classes can derive from each other.
Reference: C++ Strategies and Tactics by Robert B. Murray, Addison-Wesley, 1993., p. 75.

Wednesday, December 16, 2009

C++98: Deriving a Class from an int

You cannot derive a class from an int.
Reference: "C++ Strategies and Tactics" By Robert B. Murray. Addison-Wesley, 1993., p. 192.

Monday, December 14, 2009

C++98: Dangling Pointer

stringstream ss("MyString");const*p=ss.str().c_str(); makes p a dangling pointer. The invisible temporaries created from str() and c_str() live only until the last ';'.

It might look like it works, but if you add some more code following const*p=..., you may see your string corrupted. I just added stringstream ss2("Another String"); and this corrupted the data to which p points.

This kind of problem is fun (sarcasm, because it is not fun) to debug.

Reference: First-hand experience.

Saturday, December 12, 2009

Thursday, December 10, 2009

assign()

The function assign() is a member of the following containers: vector, string, deque, and list.
Reference: Effective STL by Scott Meyers. Addison-Wesley, 2001., p. 37.

Tuesday, December 8, 2009

STL Container Destructors

None of the Standard STL containers have virtual destructors.
Reference: Effective STL by Scott Meyers. Addison-Wesley, 2001., p. 37.

Sunday, December 6, 2009

Memory Leaks

The following statement leaks 10 bytes of memory:
operator new(10);
Reference: Effective STL by Scott Meyers. Addison-Wesley, 2001., p. 51.

Friday, December 4, 2009

STL Algorithms

These STL algorithms require that their input be sorted: binary_search, upper_bound, set_union, set_difference, merge, includes, lower_bound, equal_range, set_interaction, set_symmetric_difference, and inplace_merge.
Reference: Effective STL by Scott Meyers, Addison-Wesley, 2001. p. 147.

Wednesday, December 2, 2009

unique() and unique_copy()

The STL algorithms, unique() and unique_copy() do not require their input to be sorted. The result might be surprising though, since only duplicates that are adjacent are removed.
Reference: Effective STL by Scott Meyers, Addison-Wesley, 2001., p. 147.

Monday, November 30, 2009

Unique()

Calling unique() on a vector containing {1,2,1,2} results in {1,2,1,2}.
Reference: Effective STL by Scott Meyers. Addison-Wesley, 2001. p. 148.

Saturday, November 28, 2009

Associative Containers

For associative containers, the find member functions are O(logn), and the find algorithms are
O(n).
Reference: Effective STL by Scott Meyers, Addison-Wesley, 2001. p. 190.

Thursday, November 26, 2009

STL Header File Names

All STL containers are declared in header files of the same name except for multimap and multiset.
Reference: Effective STL by Scott Meyers. Addison-Wesley, 2001., p.209.

Tuesday, November 24, 2009

Compiler implicitly declared functions

When the compiler implicitly declares default constructors, copy constructors, copy assignment operators, and destructors; they are implicitly declared inlined.
Reference: Exceptional C++ Style by Herb Sutter, Addison-Wesley, 2004., p. 143-145.

Sunday, November 22, 2009

POD

The int type is a trivial case of a POD (Plain Old Data).PODs contribute to the compatibility between C and C++.

Friday, November 20, 2009

Deques

Deques are not required to be implemented as C-style arrays as vectors are.
Reference: Exceptional C++ Style by Herb Sutter, Addison-Wesley, 2004., p. 159.

Wednesday, November 18, 2009

Inlined Functions

A compiler can inline functions that you did not declare inline (i.e. by using 'inline' in the declaration or by providing the body in the class declaration).
Reference: Exceptional C++ Style by Herb Sutter, Addison-Wesley, 2004., p. 193.

Monday, November 16, 2009

Inlined Functions

A compiler can refuse to inline a function that you declared inline.
Reference: Exceptional C++ Style by Herb Sutter, Addison-Wesley, 2004., p. 193.

Saturday, November 14, 2009

Inline Functions

A compiler can inline a function in one place and not in another.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004. p. 193.

Friday, November 13, 2009

Static Class Member Variables

When you have a static class member variable, there is only one copy of the variable in the whole program. If you have member function with a static variable, there is also only one copy of the variable in the whole program.