Dnnnn=yy-nnnn Emil Madsen Doc No: Dnnnn=yy-nnnn Revision: 1.0 Date: 2013-11-06 Project: Programming Language C++, Library Evolution Working Group Author: Emil Madsen Reply to: Emil Madsen Email: [email protected]

A Proposal to add lambda traits to the STL Contents I

Introduction

1

II Motivation

1

III Scope

3

IV Impact On the Standard

3

V Design Decisions

3

VI Technical Specifications

3

VIIAcknowledgements

4

I

Introduction

This proposal aims to support compile-time retrieval of lambda / function return-type and parameter types, using traits. A simple usage example can be found in the motivation section.

1

II

Motivation

Note: This following discussion on lambdas also applies to ordinary functions and methods. When passing lambdas as function arguments or saving them to variables, one can either; • Pass/Save the lambda as a function pointer, assuming it’s capture-less. • Pass/Save the lambda as a std::function (when wrapped), or • Pass the lambda as a templated type / Save it using the auto keyword. The main point of the above, is that; in order to do one of the two first, one has to explicitly specify the exact type of the lambda. While the last option saves us from doing this (which may be preferable). However using the last method, leaves us in a somewhat unknown state, as to what the return-type, and parameter(s) for the lambda is. That is, currently there’s no standard way of querying this information from the lambda variable itself / template parameter. Do please note that the same applies to functions bind via unrestricted templates or the auto keyword. Being able to statically determine attributes of templated and auto saved lambdas at compile-time, will allow one to generate definitions, and generic code based upon these. In essence, this means that one can write generic code, while having template specific behavior, so the issue being solved by this proposal is alike any other issue, where traits is proposed as the solution. That is in the generic environment, where assertions and/or behavior is dependent on the template arguments. A pratictal toy example of this would be the following; template class std_function_convert { private: // This function doesn’t need a body, as it should never be called. template static auto convert_to_std_function(std::integer_sequence) -> std::function::return_type( typename function_traits::template arg::type...)>; public: using type = decltype(convert_to_std_function(typename std::make_integer_sequence::arity>())); }; 2

Note: ‘std::integer_sequence‘ and friends refer to the concepts from N3493. The above code allows one to convert a lambda to a std::function, by extracting the required template information from the lambda at compile time, and typedef’ing the resulting std::function inside a struct. Usage code may look like; auto lambda = [](int i) { return long(i*10); }; using std_function = std_function_convert::type; static_assert(std::is_same>::value, "std_function_convert is broken"); The above is of course just a simple toy example, for the purpose of this text. Plenty of other, possibly more useful examples can be thought up. Besides from the obvious implications and usage scenarios of this, it should also be added that this proposal would be an obvious fit for the type_traits C++11 header. As it applies the traits solution, not to ordinary types, but to callable types (lambdas, function pointers and methods).

III

Scope

The people who’ll likely be the users of this proposed extension to the standard library, are generally speaking library writers and generic code developers. Who are experienced C++ developers.

IV

Impact On the Standard

This proposal is a pure library extension. It does not require changes to any standard classes, functions or headers. Except if appended to the type_traits header, although a new separate header may be a great alternative, in order to refrain from changing the current headers. The suggested implementation depends on the integer_sequence, as proposed in N3493, and on std::tuple. As integer_sequence is to be included in C++14, this proposal may be candidate for that as well.

V

Design Decisions

There has not been a lot of design decisions at this point, except naming and implementation, which are open to discussions obviously.

3

VI

Technical Specifications

Possible implementation snippet, by ’kennytm’, posted on StackOverflow; // For generic types, forward to use signature of its ’operator()’ template struct function_traits : public function_traits { }; // Specialization for pointers to member functions template struct function_traits { using return_type = ReturnType; static const int arity = (sizeof...(Args)); template struct arg { using type = typename std::tuple_element>::type; }; }; Note; Full implementation, by ’kennytm’, can be found at; GitHub.

VII

Acknowledgements

I’d like to thanks ’kennytm’, for his answer on StackOverflow which eventually lead me to creating this proposal.

4

A Proposal to add lambda traits to the STL -

Date: 2013-11-06. Project: Programming Language C++, Library Evolution Working Group ... This proposal aims to support compile-time retrieval of lambda ...

141KB Sizes 2 Downloads 136 Views

Recommend Documents

A proposal to add a transcoding library to the ... -
One of the fundamental aspects of software development is the transformation of data from ... domain, many of which are patented, commercial property. It is not ...

how to add a printer.pdf
will need to download the PaperCut client here. Page 1 of 1. how to add a printer.pdf. how to add a printer.pdf. Open. Extract. Open with. Sign In. Main menu.

How to Add an Image to the Banner.pdf
Page 1 of 7. Adding an Image to the Banner. By the HSSD Tech Department. Step 1: Login to your account in upper right hand corner of the screen. Step 2: After ...

From Research Hypotheses to Practical Guidelines: A Proposal to ...
leverages the power of social networking as a vehicle for collaboration between researchers and practitioners, which will mutually benefit both parties. Conduits ...

Add a contractor or employee to G Suite
First, check your G Suite subscription to see how users are added ... Click Billing . Next to your G Suite subscription, you'll see the days remaining in your G Suite trial. 1.2. You're on a Flexible Plan. If you have the Flexible Plan, you don't nee

Perspective Probe: Many Parts add up to a Whole Perspective
Apr 9, 2009 - the conversation around a sensitive topic instead of asking directly .... probe involved having participants complete several activities on their ...

Add shift to -
not cycle with the data, the mask indices need to be updated whenever the buffer is cycled. - A programmer might need to shift elements in non-circular buffers ...

Perspective Probe: Many Parts add up to a ... - googleusercontent.com
Apr 9, 2009 - ... the sensitive topic by breaking it into pieces and having each activity add to .... One theme that emerged was that at a high level participants' ...

Perspective Probe: Many Parts add up to a ... - googleusercontent.com
Apr 9, 2009 - ... at their brokerage site or sites such as Google Finance - that allow users to ... activities, we made a list of questions that we wanted to investigate, and ..... understanding anyone's social network in 60 minutes.” Proc. Of 2007

How to Add Access to Student Data.pdf
Then, on the Modify Access page, check the box next to. “Student reports for [school]”. (Note: users with the Account. Management permission have access to all ...

Atomic Learning How to Add Content to Blackboard Presentation.pdf ...
Atomic Learning How to Add Content to Blackboard Presentation.pdf. Atomic Learning How to Add Content to Blackboard Presentation.pdf. Open. Extract.

Add shift to -
Aug 19, 2017 - This paper proposes adding shift algorithms to the C++ STL which shift elements forward or backward in a range of elements. II. Motivation and ...

G-lambda: Coordination of a Grid Scheduler and Lambda Path ...
Sep 27, 2006 - Network resource management techniques ... management system-based (management system interface has not been defined). ▫ OIF UNI 1.0/2.0 .... ▫Data management module: Stores reservation resource information.

how to make a good reserach proposal
HOW TO MAKE A GOOD RESERACH. PROPOSAL. Wenxuan Hou. 1. Manchester Business School. 2005. 1 Contact information: [email protected]. 1 ...

Research Proposal: A Unified Approach to Scheduling ...
between tasks, and data dependencies between tasks and files. .... Many different types of grid computing systems have been developed over the years.

How to add Flash movies and Flash video to a website
To add interest and interactivity, you can add movies created in Adobe Flash CS5 and Flash video to your site. Flash movies contain animated and interactive ...