jeudi 19 mars 2020

How to get a decltype of the current function return type

So I have a function (might be a template but it isn't at the moment) that has quite a messy return type - pairs, maps, unique_lock, etc all in there:

std::pair<std::map<int, MyObject>&,std::unique_lock<std::mutex>> MyContainers::getThatMapSafely()
{
    return std::pair<std::map<int, MyObject>&,std::unique_lock<std::mutex>>(some, stuff);
}

I used to just return a list-constructed result with {}, but that is no longer an option, so I need to declare the type to get the construction I want. Is there an easy way to use decltype-like syntax to save me copy and pasting this obnoxious thing into the return statement? I know I could typedef it in the class, but I feel that if I declare a typedef in the header, people reading the header see it as one more thing they have to digest, and it will only be used by this one function.

I'm aware of std::result_of, so the sequence decltype std::result_of decltype getThatMapSafely should be workable, but is getting quite long again, but is there any way without naming the current method, or presuming there has been a previous declaration?

As I always say, I'm currently bound by c++11, but interested in hope for the future.

Aucun commentaire:

Enregistrer un commentaire