Is it possible to have an “auto” member variable?

后端 未结 4 1691
别跟我提以往
别跟我提以往 2021-01-03 20:54

For example I wanted to have a variable of type auto because I\'m not sure what type it will be.

When I try to declare it in class/struct declaration it

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 21:21

    Indirectly, provided that you don't reference a member of the class.

    This can also now be achieved through deduction guides, these were introduced in C++17 and have recently (finally) support in VC++ has been added (clang and GCC already had it).

    https://en.cppreference.com/w/cpp/language/class_template_argument_deduction

    For example:

    template 
    struct CString;
    
    template 
    struct CString>
    {
        std::array const Label;
    
        CString(std::array const & pInput) : Label(pInput) {}
    };
    
    template 
    CString(std::array const & pInput) -> CString>;
    

    https://godbolt.org/z/LyL7UW

    This can be used to deduce class member types in a similar manner to auto. Although the member variables need to be Dependant somehow on the constructor arguments.

提交回复
热议问题