<<< A useful template trick     Index     Function templates >>>

8. Default constructor behavior of C++ types

  • More examples of the useful trick that we described:

    
    int x = int();       // x == 0
    
    double d = double(); // d == 0.0
    
    bool b = bool();     // b == false
    
    
    
  • 
    char* cp = char*();  // is a syntax error
    
    
  • However,

    
    #include <iostream>
    
    using namespace std;
    
    template< typename ValueT >
    class Thing {
    public:
        Thing< ValueT >()
        {
            ValueT dummy = ValueT();
            // dummy is zero if ValueT is a pointer:
            std::cout << static_cast< void* >( dummy );
        }
    };//class Thing
    
    int main()
    {
        Thing< char* > thing;
        return 0;
    }
    
    
<<< A useful template trick     Index     Function templates >>>