c++ - Forwarding cv-ref-qualifier for member functions -


if there no overloadings (say, f(t &) or f(volatile t &&)) of (member) function template template< typename t > f(t &&);, t && so-called forwarding reference, , t either u, or u & cv-qualified type u. cv-ref-qualifiers of member functions there no such rule. in struct s { void f() && { ; } }; s::f() has rvalue-reference qualifier.

in generic code useful avoid definition of 4 (or 8, if consider volatile qualifier) overloadings of member function, in cases if of them doing same thing.

another problem arises in way, impossibility define effective cv-ref-qualifier of *this in particular sense. following code not allows 1 determine whether ref-qualifier of member function operator () && of &.

#include <type_traits> #include <utility> #include <iostream>  #include <cstdlib>  #define p \ {                                                                       \     using this_ref = decltype((*this));                                 \     using this_type = std::remove_reference_t< this_ref >;              \     std::cout << qual() << ' '                                          \               << (std::is_volatile< this_type >{} ? "volatile " : "")   \               << (std::is_const< this_type >{} ? "const " : "")         \               << (std::is_lvalue_reference< this_ref >{} ? "&" : "&&")  \               << std::endl;                                             \ }  struct f {     constexpr int qual() & { return 0; }     constexpr int qual() const & { return 1; }     constexpr int qual() && { return 2; }     constexpr int qual() const && { return 3; }     constexpr int qual() volatile & { return 4; }     constexpr int qual() volatile const & { return 5; }     constexpr int qual() volatile && { return 6; }     constexpr int qual() volatile const && { return 7; }     void operator () () & p     void operator () () const & p     void operator () () && p     void operator () () const && p     void operator () () volatile & p     void operator () () volatile const & p     void operator () () volatile && p     void operator () () volatile const && p };  int main() {     {         f v;         f const c{};         v();         c();         std::move(v)();         std::move(c)();     }     {         volatile f v;         volatile f const c{};         v();         c();         std::move(v)();         std::move(c)();     }     return exit_success; } 

but nice, if there above syntax. i.e. decltype((*this)) denote exact cv-ref-qualified type of *this. not breaking-change introduce such syntax coming version of c++ standard @ mind. && forwarding cv-ref-qualifier (and looks omission of committee (namely, core language working group)).

another sequence possible denote both member function cv-ref-qualifier , cv-ref-qualified type of *this body: auto &&, decltype(&&) etc.

is there proposal regarding issue, prepared use in c++17?

yes, there such proposals.

background:

since have forwarding references in template functions, turn member function template friend function (and protect via enable_if being used other class f, if required).

now, maybe want really, want use function member function, because really, syntax better.

the proposals:

look unified call syntax proposals, instance: n4174

if accepted, able use free functions member functions of first argument. cover example code linked in first comment. admittedly, not cover operator(), consider minor nuisance compared writing 8 overloads :-)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -