Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the proposal for standardization of "extension methods" in C++?

Tags:

c++

I forgot the exact term used to describe the feature that was used in the proposal I saw about a year (maybe more) ago. it looked similar to C# extension methods in that a global function defined as part of a class's interface could be invoked using class-member syntax. Very very dumbed down example:

class Foo
{
public:
    void One();
};

void Two(Foo&);

int main()
{
    Foo f;
    f.One();
    f.Two(); // This was valid in the proposal I saw
}

I'm sure my code example is way off, but I'm using it more to try to communicate the feature I remember seeing. Am I completely off here or was there a proposal out there for C++ that introduced this mechanism? And if so, could someone point me to the proposal and possibly its status/timeline?

like image 839
void.pointer Avatar asked Apr 11 '18 14:04

void.pointer


People also ask

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.

Where we can use extension method in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

What keyword is used to define an extension method?

Extension Method uses the "this" keyword as the first parameter. The first parameter always specifies the type that the method operates on. The extension method is written inside a static class and the method is also defined as static.

How are extension methods implemented?

How to implement Extension Methods: Create a static method in a static class. Pass the type which you want to extend, as a first parameter to method. The first parameter should have the combination with keyword 'this' and 'type'


2 Answers

There is a proposal for Unified (var Uniform) Call Syntax for C++. From 10,000 feet PoV, it will allow extensions methods a la C# as well as really blending the difference between method call and function call for a given object.

Here is short description how this feature would look like

https://isocpp.org/blog/2016/02/a-bit-of-background-for-the-unified-call-proposal

As far as I know, it didn't make it into C++17 (too late?), awaiting for c++2x

like image 159
Severin Pappadeux Avatar answered Sep 28 '22 00:09

Severin Pappadeux


Roger Orr and I proposed adding extension methods to C++ (17) in 2015 in this paper:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0079r0.pdf

The (Language) Evolution group were not convinced that this was a direction they were keen to pursue.

I think that extension methods in C# are a nice feature but this does not seem to be a truth universally acknowledged.

I do not intend to re-raise the proposal for C++20.

Brief summary

(as requested by einpoklum)

Uniform call syntax proposed that member functions could be called with free function syntax:

void foo(const A& a);
class B { void foo(); };

A a;
foo(a); // calls foo(const A&);

B b;
foo(b); // calls b.foo();

Extension methods proposed that suitably written free functions could be called with member function syntax:

void foo(const A& this); // note the name `this` is important
class B { void foo(); };

A a;
a.foo(); // calls foo(const A&);

B b;
b.foo(); // calls b.foo();
like image 37
jbcoe Avatar answered Sep 28 '22 00:09

jbcoe