C++ question v. templates

Discussion in 'OT Technology' started by CaseLogic, Feb 27, 2005.

  1. CaseLogic

    CaseLogic Longhorns - Mavs - Cowboys - Tool - 24 - Lost - Th

    Joined:
    Sep 22, 2003
    Messages:
    23,451
    Likes Received:
    0
    Location:
    Austin, TX
  2. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Well, I'm not so good at templates, but from what I can see, the function prototype has one return value and you're trying to use two return values. I mean, the function prototype is
    Code:
    R<T> Generate();
    and you're trying to do
    Code:
    R<long long> x = Generate();
    That's why you're getting an error, because the function call doesn't match the function prototype. Try just doing
    Code:
    R<long> x = Generate();
    EDIT: You know what... Looking at the code again, you're gonna have to change the function prototype. You've got the function returning two values, but you're only defining one return type. It would probably be easier if you posted the function that was provided before you "templatize" it.

    Like I said, I'm not so good at templates, but the way those are written look different than the way I've seen them...
     
    Last edited: Mar 1, 2005
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    since Generate is a template function, you have to specify what type it's going to work with.

    R<TYPE> x = Generate<TYPE>();
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    long long is the typename for a 64-bit integer.
     
  5. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    Ah, never heard of them before, I guess... :dunno:
     
  6. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    You know what confuses me a little about this segment of code is the way it's written. All the sample code I've seen for templates are written differently. For example:
    Code:
    template <typename T>
    R<T> Generate();
    
    I don't understand what the R<T> is there for. Every implementation of templates I've seen would have it written as:
    Code:
    template <typename T>
    T Generate();
    
    T being the return type.

    And then, at the end of the function:
    Code:
    return R(x, y);
    
    I don't get what this is supposed to be returning. What does the R signify, and what are x and y supposed to be? I see x being defined locally as type T, but y doesn't seem to be defined anywhere. It's not coming in as a parameter, and it's not declared locally. Also, what is this line supposed to do:
    Code:
    T x = (T)rand() % 10, y;	//This range is small to avoid overflow
    
    I'm not trying to be critical or anything. I'm mainly asking these questions to help myself get a better understanding of templates, because we use them a lot at work. I haven't had to actually deal with them myself just yet, but I know there in our code and I'll have to deal with them eventually.
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I think R is a templated class. The Generate function returns an instance of R, and the last line probably needs to be return R<T>(x,y);
     

Share This Page