Barry Revzin [email protected]

Make Pointers to Member Functions Callable Introduction

Member functions play an important role in C++. However, pointers to members are still second-class citizens to pointers to non-members. They’re INVOKE-able, but not actually invokable. As a result, lots of code exists to handle this exceptional case. But there’s nothing exceptional about pointers to member functions. Or at least, there shouldn’t be.

Motivation Consider the following declarations: struct X { int member(int ); };

int non_member(X*, int);

auto p_mem = &X::member; auto p_fun = &non_member;

While the functions non_member() and member() have different rules for accessing members of X, on the call side, they’re both still functions. And yet, we have radically different syntax rules for simply calling them: X x; (x.*p_mem)(1); p_fun(&x, 2);

The second one is just a normal function call, the first one is a mess. The syntax is difficult for beginners to grok, and even experienced C++ programmers find it cryptic. This different call syntax also means that we can’t use pointers to member functions directly in any of the standard algorithms. As Scott Meyers laments in Item 41 of Effective STL:

In a perfect world, I’d also be able to use for_each to invoke Widget::test on each object in vw: for_each(vw.begin(), vw.end(), &Widget::test); // Call #2 (won’t compile)

In fact, if the world were really perfect, I’d be able to use for_each to invoke Widget::test on a container of Widget* pointers, too:

list lpw; for_each(lpw.begin(), lpw.end(), &Widget::test); // Call #3 (also won’t compile)

For these cases, we now have std::mem_fn. But that’s boilerplate that just hides what is going on, and hardly makes for a “perfect world.” There already exists a clear desire to make pointers to member functions more uniform with all the other INVOKE-able objects. We can put either member functions or non-member functions inside of a std::function, or passed to std::bind or the newly accepted std::invoke: std::function f1 = &X::member; std::function f2 = non_member; int b1 = std::bind(&X::member, &x, 1)(); int b2 = std::bind(non_member, &x, 2)(); int i1 = std::invoke(&X::member, &x, 1); int i2 = std::invoke(non_member, &x, 2);

In Eric Niebler’s range-v3 library, the predicates and functions passed to algorithms are wrapped in the library side so that the difference is transparent to the user. But all of these cases simply shift the burden from the user to the library developer to handle this case separately. It would be far simpler to just do away with the special case entirely.

Proposal Rather than either placing the burden on the user to know how to handle pointers to member functions, or placing the burden on the library implementers to take care of everything for the user, or suggesting to rewrite everything in , this paper is simply proposing to make all INVOKE-ables actually invokable by supporting the “normal” call syntax for pointers to member functions. That is: X x; p_mem(&x, 1); // equivalent to (x.*p_mem)(1);

The same would apply to pointers to member data: struct Y { int val; };

auto p_val = &Y::val; Y y{42}; p_val(y) = 17; // equivalent to (y.*p_val) = 17; // now y.val == 17

This syntax is natural and intuitive. You already know how to interpret it correctly. There are no surprises here. [func.require] currently defines the concept of INVOKE. I am simply proposing that we equate the meaning of f(t1, t2, ..., tN) with INVOKE(f, t1, t2, ..., tN).

Note that this proposal is not related to the unified call syntax proposal. The latter proposal is about supporting the syntax member(&x, 1) – calling member functions by name using the non-member call syntax. This proposal is about calling pointers to members.

Impact In most cases, the adoption of this proposal will not affect existing code. This paper is not proposing to remove the current pointer-to-member call syntax. The implementations of the aforementioned std::function, std::bind, and std::invoke could change to be simpler (but don’t have to). std::mem_fn would effectively become obsolete. There may be examples in code which use SFINAE to determine if an object is callable that previously rejected pointers to member functions which may now break. This paper suggests that the breakages, such as they are (and there may not be many), are worth it in order to simplify the language rules. The syntax becomes more regular, more predictable, more uniform.

Make Pointers to Member Functions Callable -

auto p_fun = &non_member;. While the functions non_member() and member() have different rules for accessing members of X, on the call side, they're both still ...

506KB Sizes 4 Downloads 137 Views

Recommend Documents

Pointers
0x0. 0x1. 0x4. 0x5. 0x6. 0x9. 0x8. 0xA. 0xB. 0x2. 0x7. 0x3. Memory. Page 3. Page 4. Creating Pointers. Declaring pointers: ... What will print? int main(void). { char* str = "happy cat"; int counter = 0; for (char* ptr = str; *ptr != '\0'; ptr++). {

HotCopper Member and Prospective Member Survey.pdf
Page 1 of 4. 14/11/2012 Página 1 de 9 Profesor: Luís Rodolfo Dávila Márquez CÓDIGO: 00076 UFPS. CURSO: CÁLCULO INTEGRAL. UNIDAD 2 A.

Pointers and Memory
18 classic linked list problems with solutions — a great way to practice with realistic, pointer intensive C code, ... How local variables and parameters work: local storage, allocation, deallocation, the ampersand bug. .... It turns out to be conv

MEMBER NEWSLETTER
Chicago tackles issues we all face in our own communities. .... Apple icon Steve Jobs, some of his more ... before beginning his own company,. Independent Oil ...

member newsletter
Members met with 26 military servicemen who live at The Pathway. Home, a residential recovery program for our country's “New Warriors” - those who have survived the stressors of war but may experience difficulty transitioning. The L3ers and soldi

Member Register -
Closure. Chief Judge and Contest Chair. 10:00. 12:35 AM. 12:45 AM. Gurgaon Toastmasters Club. Toastmasters Club # 1200975. 429th Meeting - Sep 3rd, 2017 (Sunday). Time: 9:00 AM - 12:45 PM. Address: The Shri Ram School , V- 37, Moulsari Avenue, DLF Ph

Member Register -
Suraj Singh. 02:30. 12:06 PM. 12:09 PM. 09911818104 [email protected]. GE calls for Parliamentarian. Parliamentarian's Report. TBD. 02:30. 12:09 PM.

member newsletter - L3 Organization
California and everyone who attended just .... experience and all said they plan on doing it again. We believe ... Cardiothoracic Surgeon from Baylor College of.

Member Eligibility.pdf
Sign in. Page. 1. /. 1. Loading… Page 1. Member Eligibility.pdf. Member Eligibility.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Member Eligibility.pdf. Page 1 of 1.Missing:

member newsletter Accounts
Side” by Jo Robinson. Kim says: “It is an outstanding book about how ..... I was in college in 1967 and read that the Arno River had overflowed and Florence was ...

member newsletter - L3 Organization
Cardiothoracic Surgeon from Baylor College of. Medicine ... the arts. And we lived vicariously through explorers from National ... 23, again in Naples, Florida. It.

member newsletter
forward to continuing to build up L3 as a strong ... learning in treatment.” To learn more about the trip, please visit www.L3.org/member/ viewevent/86. Cont. from HOUSTON. Above: Dr. Brent Forester, MD, MSC, from McClean Hospital .... The L3 App i

Member Register -
Project # 1. Speaker 1. Rahul Agrawal. 06:30 ... Project # 1. Speaker 2. Sakshi Madan .... Theme - "UFO and Aliens Mystery". Guest Registration & Members ...

Submodular Functions: from Discrete to Continuous Domains
Extension to continuous domains. – Application: proximal operator for non-convex regularizers. • Preprint available on ArXiv, second version (Bach, 2015) ...

Introduction to number theoretic functions
Jan 14, 2018 - generated by relatives of the Riemann zeta function, but there is a somewhat breathtaking general- ization that takes in all of these at a single ...

Rivan member namelist.pdf
Page 1 of 3. S/No NAME VILLAGE CONTACT No. 1 Mr.Suresh Gurung Rivan [email protected] 07428942575. 2 Mrs.Geeta Gurung Rivan. 3 Mrs.Bhaikhu Gurung(mother) Rivan. 4 Son .Prosan Gurung Rivan. 5 Dau.Lalita Gurung Rivan. 6 Mr.Rajendra Gurung Riva

Applications of Homogeneous Functions to Geometric Inequalities ...
Oct 11, 2005 - natural number, f(x) ≥ 0, yields f(tx) ≥ 0 for any real number t. 2. Any x > 0 can be written as x = a b. , with a, b ... s − b, x3 = √ s − c, yields f. √ s(. √ s − a,. √ s − b,. √ s − c) = △. Furthermore, usi

Member Application / Renewal
Individual Membership: $10. Student/Parent/Retiree: $10 (Full---time students, retired educators, non---educator parents). School Group Memberships: $150.

Minnesota GreenCorps Member! -
The MPCA and MN GreenCorps do not discriminate with regard to race, sex, color, creed, religion, age, national origin, disability, marital status, familial status, ...

EZ Member Directory.pdf
Sign in. Page. 1. /. 2. Loading… Page 1 of 2. Page 1 of 2. Page 2 of 2. Page 2 of 2. EZ Member Directory.pdf. EZ Member Directory.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying EZ Member Directory.pdf.

SkyBoundary: An improved Approach to Member ...
this paper: (1) the virtual points which are needed for the ..... Core(TM)2 Duo CPU T7300 @ 2.0GHz machine with 1. Gbytes Ram and 120 Gbytes Hard disk running Windows. XP. Datasets: 1)Co-author: The dataset2, the entire version of.

Wellfield Member Discounts.pdf
Page 1 of 1. Результат запроса: Стихи на молдавском языке о маме. Page 1 of 1. Wellfield Member Discounts.pdf. Wellfield Member Discounts.pdf. Open.