Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

Which is better style?

This topic is 3051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Which is better style, 1)
string str = convert<string>(123,BASE_16);
where convert has the following prototype :
template<typename ReturnType, typename InputType>
ReturnType convert(const InputType, const size_t base);
or this way : 2)
string str;
convert(123,str,BASE_16);
where the convert function has the following prototype :
template<typename ReturnType, typename InputType>
void convert(const InputType src,ReturnType& dest, const size_t base);
I feel like the second one looks better.

Share this post


Link to post
Share on other sites
Advertisement
I personally favor #1 strongly. Functions exist to take params and return results. Mixing them up in the parameter list is (imo) distasteful.

Share this post


Link to post
Share on other sites
It depends. Can the operation fail? It often makes sense to use an output parameter for the result and reserve the return value for indicating success/failure, most often in cases where a) the output itself can't be used to reliably detect success/failure, and b) you can't use exceptions. I prefer #1 when I have a choice, though.

Share this post


Link to post
Share on other sites
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.

Share this post


Link to post
Share on other sites
Quote:
Original post by theOcelot
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.


One would use #2 when trying to avoid redundant heap allocations when parsing a lot of text, perhaps hundreds of thousands of lines, thereby reducing running time by a factor of 10.

YMMV, depends on implementation of std::string and whether RVO/NRVO can handle objects which invoke new in constructor.

Share this post


Link to post
Share on other sites
If you really expect to need #2 in specific circumstances for performance reasons, you can always implement #1 in terms of #2, and provide both:


// Ugly version for people who have identified a need
template<typename ReturnType, typename InputType>
void convert(const InputType& src, ReturnType& dest, const size_t base) {
// evil conversion logic
}

// Pretty version for people who like pretty code
template<typename ReturnType, typename InputType>
ReturnType convert(const InputType& src, const size_t base) {
ReturnType result;
convert(src, result, base);
return result;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
Original post by theOcelot
What on earth do you prefer #2 for? The only time I consider anything like that is when I don't want to copy some big object for a return value, and even then creating and initializing the variable on two lines is annoying.


One would use #2 when trying to avoid redundant heap allocations when parsing a lot of text, perhaps hundreds of thousands of lines, thereby reducing running time by a factor of 10.


Sure, other efficiency issues. But Concentrate seems to prefer it aesthetically. That's what I don't get.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!