Sign in to follow this  

DRY principle and return type inference

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
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

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