# DRY principle and return type inference

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

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