Monday, October 4, 2010

Double const Allowed in Template

The following compiles:

       template <typename T>
       class ConstType {
        public:
          typedef const T MyConst;
       };
       ConstType<const int>::MyConst i = 0;

Note the double const, and the compiler does not complain about it.

Reference: Modern C++ Design by Andrei Alexandrescu. Addison-Wesley, 2001, p. 37.

Using sizeof to Find Type Information at Compile-Time

sizeof allows you to find type information at compile time.

Reference: Modern C++ Design by Andrei Alexandrescu. Addison-Wesley, 2001, p. 34.

See the above reference for fancy examples.

Sunday, October 3, 2010

std::type_info

std::type_info allows you to find type information at run-time, not compile time.

Reference: Modern C++ Design by Andrei Alexandrescu. Addison-Wesley, 2001, p. 37.

Saturday, October 2, 2010

The typeid() Function

The Standard C++ Library function typeid() maps a class name to a string. The mapping is not standardized. Typically, the output is the class name as a string; but it could be "" and still be compliant.

Reference: Modern C++ Design by Andrei Alexandrescu. Addison-Wesley, 2001, p. 39.

References to Pointers

Function formal parameters may be of type 'reference to a pointer', but not of type 'pointer to a reference'.

Friday, October 1, 2010

Pass by Pointer vs. Pass by Reference

Even though you can pass an object by reference as a reference, if you want to change the object in a function; there is a convention of making the parameter a pointer to the object instead of a reference to the object to emphasize the fact that the object is an output parameter.

This is only a convention that some people use. The nice thing about this convention is that an output parameter is made obvious in the caller and the callee. You can argue that lack of 'const' may tell you it is an output parameter from the callee side, but: 1) it won't be shown on the caller side; and 2) parameters passed by value typically do not use the 'const' specifier.

References to References

You can't have a reference to a reference.

Reference: Modern C++ Design by Andrei Alexandrescu. Addison-Wesley, 2001, p. 43.