Template functions

Started by
7 comments, last by JohnBolton 18 years, 4 months ago
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 DECLARATION
template <typename tType>
const tType max(const tType & x, const tType & y);

// TEMPLATE IMPLEMENTATION
template <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 DECLARATION
template <typename tType>
tType const & max(tType const & x, tType const & y);

// TEMPLATE IMPLEMENTATION
template <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]
Advertisement
Answer

Personally, 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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Thanks for the link and the advice.
Quote:Original post by stylin
It 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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
Quote:Original post by stylin
It 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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by stylin
Quote:Original post by Promit
Quote:Original post by stylin
It 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.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Quote:Original post by Mike2343
Quote:Original post by stylin
Quote:Original post by Promit
Quote:Original post by stylin
It 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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
There are two advantages to "type const variable" and "type const * variable":
  1. The description maps directly (in reverse order) to the declaration: "variable is a const type" and "variable is a pointer to const type".
  2. 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".
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement