Template Metaprogramming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Imposter
    New Member
    • Dec 2011
    • 3

    #1

    Template Metaprogramming

    Hi, I was going through the TMP . I got stuck with the following code . I coundn't understand the declaration of
    Code:
    Class if_
    in the following code ,can anyone help me in figuring out the FLOW OF THE PROGRAM

    Code:
    template <bool Condition, typename TrueResult, typename FalseResult>
    class if_;
     
    template <typename TrueResult, typename FalseResult>
    struct if_<true, TrueResult, FalseResult>
    {
      typedef TrueResult result;
    };
     
    template <typename TrueResult, typename FalseResult>
    struct if_<false, TrueResult, FalseResult>
    {
      typedef FalseResult result;
    };
     
    int main()
    {
      typename if_<true, int, void*>::result number(3);
      typename if_<false, int, void*>::result pointer(&number);
     
       typedef typename if_<(sizeof(void *) > sizeof(uint32_t)), uint64_t, uint32_t>::result
          integral_ptr_t;
     
       integral_ptr_t converted_pointer = reinterpret_cast<integral_ptr_t>(pointer);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    class if_;
    is called a forward reference.

    The rule is you cannot use a class name unless the class has already been defined. Th forwrd reference says the class has been defined bu just not in this file o in ts includes.

    This assurance by you that class if_ exists somewhere is enough for the compiler to allow and if_* or an if_&. However, if you start executing member functons of if_ you will need the full class definiion.

    Usually you see this where the details of if_ need to remain hidden because if_ is a commercial purchase and the seller does not want you to know how if_ is designed. So you are given a header with a forward reference and a library. The library was built using the complete definition soit's OK for you to call a library function using an if_* or an if_& because it is the library code (not you) that calls the member function.
    Last edited by weaknessforcats; Jul 13 '12, 02:28 PM. Reason: typo

    Comment

    • Imposter
      New Member
      • Dec 2011
      • 3

      #3
      hi ..
      when you say IF_* , and IF_& , WHAT DOES IT ESSENTIALLY mean... does it mean if_pointer and if_address(refe rence)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        if_ is the name of your class:

        Code:
        class if_;
        so if_ is a type like char is a type. Therefore, if_* is a pointer to if_ just like char* is a pointer to char. The & is a reference: if_& means reference to if_ just like char& is a reference to char.

        BTW: Sorry for the delay but there have been site problems norifying me of posts needing replies.

        Comment

        Working...