   Template functions
  Can someone please tell me which is the preferred way to write a template function based on the 2 examples supplied? Both of these compile and produce the same results, and I understand the differences between them, but is one better than the other? Thanks.[SOURCE]// TEMPLATE DECLARATIONtemplate <typename tType>const tType max(const tType & x, const tType & y);// TEMPLATE IMPLEMENTATIONtemplate <typename tType>const tType max(const tType & x, const tType & y){// IF x < y THEN USE y ELSE USE x    return  x < y ? y : x;}[/SOURCE][SOURCE]// TEMPLATE DECLARATIONtemplate <typename tType>tType const & max(tType const & x, tType const & y);// TEMPLATE IMPLEMENTATIONtemplate <typename tType>tType const& max(tType const & x, tType const & y){// IF x < y THEN USE y ELSE USE x    return  x < y ? y : x;}[/SOURCE]
AnswerPersonally, I prefer the first form because it is what I see most often.  Changing the position makes me think of tType& const.CM   ;  It depends on what types x and y are, and how expensive they are to copy.;  Thanks for the link and the advice.   ;Quote:Original post by stylinIt depends on what types x and y are, and how expensive they are to copy. Um, what? The two examples produce identical code. Note the position of the const.Anyway, the former is more common. There's no real convention.;Quote:Original post by PromitQuote:Original post by stylinIt depends on what types x and y are, and how expensive they are to copy. Um, what? The two examples produce identical code. Note the position of the const.Anyway, the former is more common. There's no real convention.Unless my eyes lie, in the first version he's returning the result by value, although I'm probably way off.   ;Quote:Original post by stylinQuote:Original post by PromitQuote:Original post by stylinIt depends on what types x and y are, and how expensive they are to copy. Um, what? The two examples produce identical code. Note the position of the const.Anyway, the former is more common. There's no real convention.Unless my eyes lie, in the first version he's returning the result by value, although I'm probably way off.I see the same thing.;Quote:Original post by Mike2343Quote:Original post by stylinQuote:Original post by PromitQuote:Original post by stylinIt depends on what types x and y are, and how expensive they are to copy. Um, what? The two examples produce identical code. Note the position of the const.Anyway, the former is more common. There's no real convention.Unless my eyes lie, in the first version he's returning the result by value, although I'm probably way off.I see the same thing. Ah, you're right. I don't think that's what he was getting at though, I think that's just a typo.;  There are two advantages to "type const variable" and "type const * variable":The description maps directly (in reverse order) to the declaration: "variable is a const type" and "variable is a pointer to const type".It is more consistent and it prevents this kind of confusion:typedef char * char_ptr;    const char_ptr ccp = 0;    ...    ccp = "Hello world!";  // oops! ccp is char * const, not const char *  // "char_ptr const ccp;" would have been clear Unfortunately, most people learn it as "const type variable" and "const type * variable".   