Thread: Weird template usage - any ideas?

  1. #1
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226

    Weird template usage - any ideas?

    Hi everyone,

    I am having problems understanding the following piece of code. In particular, I don't understand the lines,

    template<class T, T t = T()>

    and,

    template<bool b>

    Any help would be highly appreciated.

    Thanks,
    Zeeshan

    Code:
    #include <iostream>
    
    template<class T, T t = T()>
    class A
    {
    private:
        template<bool b>
        class B
        {
        public:
            static const int m_n = b ? 1 : 0;
        };
    
    public:
        static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
    };
    
    int main()
    {
        std::cout << A<int, -9>::m_value
                  << A<bool, true>::m_value
                  << A<char>::m_value << std::endl;
    
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Basically what's happening is that T t = T() will resolve into an object of type T. This value can either be a default constructed object, or it can be supplied by you. When this is compiled it will compute the m_value.

    Code:
    template<class T, T t = T()>
    Notice that this expression in this context is a default value.

    We can figure it out by drilling down the examples.
    Code:
    A<int, -9>::m_value
    //We have to substitute this: static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
    B<(-9 > 0)>::m_n - B<(-9 < 0)>::m_n
    So what is B, and m_n, and what is m_value? Well, if we look:
    Code:
     template<bool b>
        class B
        {
        public:
            static const int m_n = b ? 1 : 0;
        };
    The template parameter is just a value we use. So we just substitute again.
    Code:
    B<(-9 > 0)>::m_n - B<(-9 < 0)>::m_n
    B<false>::m_n - B<true>::m_n;
    0 - 1
    -1
    m_value should be -1.

    The others will be left for you to figure out.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Template parameters can be types, or integral values known at compile time. The template takes a type parameter T, and some integral parameter t of type T. The parameter t will default, if needed, to the default value of type T, which is 0 for integer types and false for bool type.

    Proper instantiations of the type might be A<int, 2> or A<int>. In the latter case, the second template parameter defaults to 0, the default constructed value of type int.

    T can't be any type here, it must be an integral type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Template parameters can be types, or integral values known at compile time. The template takes a type parameter T, and some integral parameter t of type T. The parameter t will default, if needed, to the default value of type T, which is 0 for integer types and false for bool type.

    Proper instantiations of the type might be A<int, 2> or A<int>. In the latter case, the second template parameter defaults to 0, the default constructed value of type int.

    T can't be any type here, it must be an integral type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird template compilation error with VisualStudio 2005
    By MarkZWEERS in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2008, 12:11 PM
  2. Template usage
    By Todd88 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2008, 01:25 AM
  3. Weird <template> syntax error
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 03:35 PM
  4. Template usage
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-15-2005, 07:07 PM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM