[英]How to use trailing return type with a templated class member
I'm trying to implement the following class: 我正在尝试实现以下类:
template <typename Container>
class reverse_adaptor
{
public: // Construction
reverse_adaptor(Container &container) :
m_container(container)
{}
public: // STL container static polymorphism
auto begin() const -> decltype(m_container.rbegin())
{
return m_container.rbegin();
}
auto end() const -> decltype(m_container.rend())
{
return m_container.rend();
}
private: // Members
Container &m_container;
};
The reason I'm using the trailing return type is because I don't know if m_container will be const or not, so I let the compiler work it out for me. 我使用尾随返回类型的原因是因为我不知道m_container是否为const,所以我让编译器为我解决。 However, I'm getting the following compiler errors:
但是,出现以下编译器错误:
/Users/mark/Blah/Stdx.h:77:40: No member named 'm_container' in 'stdx::reverse_adaptor > >'
/Users/mark/Blah/Stdx.h:77:40:“stdx::reverse_adaptor>>”中没有名为“ m_container”的成员
I thought it might be related to the multi-stage pass of templated types, so changed it to read decltype(this->m_container.rbegin())
, but that didn't work either. 我认为这可能与模板类型的多阶段传递有关,因此将其更改为
decltype(this->m_container.rbegin())
,但这也不起作用。
How can I get this to work? 我该如何工作?
Example - http://ideone.com/ekVYlH 范例-http://ideone.com/ekVYlH
The trailing-return-type of a function is part of its “ signature ” (declaration), not of its “body” (definition), and as such, it only sees names that were declared before . 函数的尾随返回类型是其“ 签名 ”(声明)的一部分,而不是其“主体”(定义)的一部分,因此,它仅看到之前声明的名称。
At the point where you declare your begin
member function, m_container
hasn't been declared yet. 在声明
begin
成员函数时,尚未声明m_container
。 (Note that the issue is not specific to template classes.) (请注意,该问题并不特定于模板类。)
You could move the declaration of m_container
up in the class definition (but it forces you to put private members before public interface, which is contrary to common practice...). 您可以在类定义中上移
m_container
的声明(但它会迫使您将私有成员放在公共接口之前,这与通常的做法相反……)。
You can work-around with declval
: replace m_container
with std::declval<Container&>()
inside the decltype
: http://ideone.com/aQ8Apa 您可以变通与
declval
:更换m_container
用std::declval<Container&>()
内decltype
: http://ideone.com/aQ8Apa
(As said in the comments, in C++14 you'll be able to drop the trailing return type and just use decltype(auto)
as the “normal” return type.) (如评论中所述,在C ++ 14中,您可以删除尾随的返回类型,而只需使用
decltype(auto)
作为“常规”返回类型。)
Addendum: As for why you can use not-yet-declared members inside the in-class body of a member function but not in the trailing return type, it's because the following: 附录:关于为什么可以在成员函数的类内主体中使用尚未声明的成员,但不能在尾随返回类型中使用的原因,原因如下:
class Foo {
public:
auto data() const -> decltype(m_data)
{
return m_data;
}
private:
SomeType m_data;
};
will be [disclaimer: informal wording!] kind of “rewritten by the compiler” into something equivalent to this: 将是[免责声明:非正式用语!]这种“由编译器重写”的形式,它等同于以下内容:
class Foo {
public:
inline auto data() const -> decltype(m_data); // declaration only
private:
SomeType m_data;
};
// at this point, all members have been declared
inline auto Foo::data() const -> decltype(m_data) // definition
{
return m_data;
}
where a name cannot be used before its declaration (which the first decltype(m_data)
violates). 在声明之前不能使用名称的地方(第一个
decltype(m_data)
违反)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.