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
错误C2227: '->秒'左边必须指向类/struct/union/泛型类型
1 个解决方案
#1
5
You have to either move the definition of a
up:
你必须把a的定义往上移动
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
:
或者用std::代替正确类型的代词和表达式:
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:
您的decltype()表达式是查找const char[4]而不是std: string const &。所以最正确的版本应该是:
using T = decltype(std::declval<SomeType&>().find(
std::declval<std::string const&>()
)->second);
#1
5
You have to either move the definition of a
up:
你必须把a的定义往上移动
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
:
或者用std::代替正确类型的代词和表达式:
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:
您的decltype()表达式是查找const char[4]而不是std: string const &。所以最正确的版本应该是:
using T = decltype(std::declval<SomeType&>().find(
std::declval<std::string const&>()
)->second);