DEV Community

csm
csm

Posted on

trailing return types in cpp

I know that C++ has different versions (standards) and
and from C++11 its called modern C++, because it got many features that made C++ feel like a brand new language or simply modern.

And it did all this while maintaining backward compatibility!
Still, there are many things people don't like about C++.
For me, especially the lengthy return types that hide the function names.

For example, suppose you have a function like this:

std::tuple<std::vector<Diagnostic>, std::vector<std::string>,
           std::unordered_map<std::string, std::string>,
           std::unordered_map<std::string, std::string>>
parse(std::vector<std::string> &args){}
Enter fullscreen mode Exit fullscreen mode

😅 Can you see where the function name is?

I've always wanted a feature like the one in modern languages such as Rust and Go, where you write the return type after the function's parameters.

Guess what? All this time, I didn't know C++ had supported trailing return type syntax since C++11!

Now the same above function, using trailing return types, would look like this:

auto parse(std::vector<std::string> &args) -> std::tuple<
    std::vector<Diagnostic>, 
    std::vector<std::string>,
    std::unordered_map<std::string, std::string>,
    std::unordered_map<std::string, std::string>
> {
    // Function body
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

It has happened many times that C++ already had the features I needed. I just didn't know they existed!

Top comments (2)

Collapse
 
technogamerz profile image
𝐓𝐡𝐞 𝐋𝐚𝐳𝐲 𝐆𝐢𝐫𝐥 •

Awesome ❤️🔥

Collapse
 
effessdev profile image
EffessDev •

Yes! 🤯