Sign in to follow this  
kelaklub

Template functions

Recommended Posts

kelaklub    160
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]

Share this post


Link to post
Share on other sites
Promit    13246
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.

Share this post


Link to post
Share on other sites
stylin    758
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.

Share this post


Link to post
Share on other sites
Mike2343    1202
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.

Share this post


Link to post
Share on other sites
Promit    13246
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.

Share this post


Link to post
Share on other sites
JohnBolton    1372
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".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this