Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

DRY principle and return type inference

This topic is 2554 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

Is there anyway in C++ to avoid specifying the return type of a function?

Say you have a function like

auto Foo(arguments) -> decltype(complicated expression here)
{
return complicated expression here;
}


The problem is that you have to repeat the complicated expression twice, which violates the principle of Don't Repeat Yourself. Is there anyway to avoid this? The only thing I can think of is to use a macro, but preprocessor macros are generally an ugly solution and it makes the code less readable as well.

Share this post


Link to post
Share on other sites
Advertisement
I would say having to specify the type is a good thing. It allows the compiler to complain if you are not returning the type you think you are returning. Also, people using your function sometimes can get away with reading the prototype only, and a prototype of a function that returns "auto" wouldn't be very informative.

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!