简体   繁体   English

如何使用带模板的类成员的尾随返回类型

[英]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_containerstd::declval<Container&>()decltypehttp://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)违反)。

The name m_container has not been introduced at the point the functions are declared . 声明函数时尚未引入名称m_container Switch the order of declarations, and the code will work. 切换声明的顺序,代码将起作用。

See working version here . 这里查看工作版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 成员函数的模板化类和类型返回类型 - Templated class and type return type for a member function 类模板化的成员函数和返回类型推断 - Class templated member function and return type inference 模板化类的专门继承会导致成员函数返回模板化类类型而不是继承类类型 - Specialized inheritance of a templated class causes member function to return templated class type rather than inherited class type 根据单独的模板化成员来改变类的模板化成员函数的返回类型 - Varying the return type of a templated member function of a class based off of a separate templated member 在将decltype与带有尾随返回类型语法的模板化成员函数一起使用时,gcc中出现编译器错误,但没有clang - compiler error in gcc but not clang when using decltype with templated member function with trailing return type syntax 如何根据模板化类中的T初始化类型的静态const成员? - How to initialize a static const member of type depending on T in a templated class? 如何根据类型获取模板化 class 成员 function 差异化? - How to get templated class member function differentiation based on type? 如何在模板化类型上专门化模板类的静态成员? - How do I specialize a static member of a template class on a templated type? 在CRTP中推断返回类型的模板化成员函数 - Inferring return type of templated member functions in CRTP 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM