How to use decltype in this case

Yves

Here is my code:

class Test
{
private:
    SomeType a;
public:
    using TE = decltype(a.find("abc")->second);
    TE getElement(const string &) const;
};

Test::TE Test::getElement(const string & key) const
{
    return a.find(key)->second;
}

In the class, a function can return an element of a like above. The type of a.find(key)->second is very complicated so I don't want to type it all. In fact, I even don't know how to type it...

So I want to use decltype like above but failed. Here is the error:

error C2227: left of '->second' must point to class/struct/union/generic type

Barry

You have to either move the definition of a up:

class Test
{
private:
    SomeType a;
public:
    using T = decltype(a.find("abc")->second);
    ...
};

Or, instead of a, substitute and expression of the correct type using std::declval:

class Test
{
public:
    using T = decltype(std::declval<SomeType&>().find("abc")->second);
    ...
private:
    SomeType a;
};

Note that you're missing a > here:

    return a.find(key)-second;
                     ^^^

And that your decltype() expression is looking up a const char[4] instead of a std::string const &. So the most correct version would be:

using T = decltype(std::declval<SomeType&>().find(
                       std::declval<std::string const&>()
                       )->second);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related