当类成员涉及c ++ 11时如何使用自动返回和decltype? [英] How to use auto return and decltype when class members involved with c++11?
本文介绍了当类成员涉及c ++ 11时如何使用自动返回和decltype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如
struct A
{
auto count() - > decltype(m_count){return m_count; }
int m_count;
};
上述获取编译错误,因为m_count在 decltype
无法识别。如何解决它? auto
返回并从 m_count
获取类型。
订单更改时代码会编译
struct A
{
int m_count;
auto count() - > decltype(m_count){return m_count; }
};
但是如何让第一种情况下工作?
decltype >中声明的名称。 code>用于尾随返回类型。因此,您必须重新排序您的声明:
struct A
{
int m_count;
auto count() - > decltype(m_count){return m_count; }
};
For example
struct A
{
auto count() -> decltype(m_count) { return m_count; }
int m_count;
};
The above gets compilation error because m_count in decltype
is not recognized. How to work around it? auto
return and get the type from m_count
must be used.
The code compiles when the order is changed
struct A
{
int m_count;
auto count() -> decltype(m_count) { return m_count; }
};
but how do I get the first case to work?
解决方案
In C++, you can't use a name that hasn't been introduced (declared) in a declaration, including in a decltype
for a trailing return type. So you must reorder your declarations :
struct A
{
int m_count;
auto count() -> decltype(m_count) { return m_count; }
};
这篇关于当类成员涉及c ++ 11时如何使用自动返回和decltype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文