Coffeehouse Thread

48 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C++ needs extension methods

Back to Forum: Coffeehouse
  • User profile image
    SteveRichter

    doing some C++ work ... c++ would be better with extension methods.

    Working with a C++ class library. One class has a function that returns the position and size of a window.
    void  GetWindowRect(long *XPos, long *YPos, long *Width, long *Height) const; 

    But I need the window info as a RECT. In C# that is no problem. Just code an extension method. No can do in C++.  Even the simple process of organizing your code is a bit of a chore in C++ compared to C#.  As I code the helper functions which will work like extension methods, where to place the code? I do not think I can add sub folders to a c++ project like I can in C#.

    -Steve

     

  • User profile image
    evildictait​or

    This sounds like something you should take up with the C++ standards committee. They decide how the language works.

    In the meantime you can just use a static method:

    __inline void GetWindowLongFromObject(__in Object* o, __out RECT* rect)
    {
      o->GetWindowRect(&rect->x, &rect->y, &rect-.w, &rect->h); 

  • User profile image
    SteveRichter

    , evildictaitor wrote

    {
      o->GetWindowRect(&rect->x, &rect->y, &rect-.w, &rect->h); 

    neat. Drop the values directly into the RECT struct.  Only problem is a WIN32 RECT does not contain a height and a width. But I like the idea.

    thanks,

     

  • User profile image
    SteveRichter

    , evildictaitor wrote

    This sounds like something you should take up with the C++ standards committee. They decide how the language works.

    I would rather Microsoft go the route of Microsoft extensions to the standard..  And I assume the organization of a C++ project is not something the C++ standards commitee deals with.  I asked about organizing code in a project with sub folders.
    http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/f9d51ab4-a7ff-46e2-9471-143536e0d2be

    You can add filters to C++ project source files and header files. They are kind of like logical folders in that they organize the code for you, but do not actually create a sub folder in the file system.

     

  • User profile image
    evildictait​or

    Raymond Chen puts it better than I can (http://blogs.msdn.com/b/oldnewthing/archive/2012/01/13/10256146.aspx):

    I always find it interesting when people say "I wish that Microsoft would stop following standards,"

  • User profile image
    Ion Todirel

    Extension methods is one of my least favorite features in C#, they are a hack to inject functionality into something that doesn't have it. Mixins can be a better choice sometimes, and you can do that in C++.

  • User profile image
    SteveRichter

    , Ion Todirel wrote

    Extension methods is one of my least favorite features in C#, they are a hack to inject functionality into something that doesn't have it. Mixins can be a better choice sometimes, and you can do that in C++.

    a mixin is created by creating a new class that is based on the class being extended?

    http://www.jakesee.com/2009/04/c-mixins-a-powerful-concept/

    I have a class library that is returning an instance of a class. How to apply the mixin pattern to an object that already exists?

     

  • User profile image
    SteveRichter

    , evildictaitor wrote

    Raymond Chen puts it better than I can (http://blogs.msdn.com/b/oldnewthing/archive/2012/01/13/10256146.aspx):

    I always find it interesting when people say "I wish that Microsoft would stop following standards,"

    no, I want Microsoft to adhere to standards. But I also want them to provide extensions. in my case, the code I use comes from all platforms. The code I write only needs to run on Windows.

    Edit:  I want Microsoft to make it easier for me to write and organize my code.  As it is, using C++ is more wrestling than coding.

     

     

     

     

  • User profile image
    exoteric

    , SteveRichter wrote

    *snip*

    a mixin is created by creating a new class that is based on the class being extended?

    http://www.jakesee.com/2009/04/c-mixins-a-powerful-concept/

    I have a class library that is returning an instance of a class. How to apply the mixin pattern to an object that already exists?

     

    Indeed, mixins, at least the mixins I've seen are no substitute for extension methods at all.

    I like the |> pipe operator in F# but in terms of tool and user friendliness, nothing beats extension methods. Dot dot dot...

  • User profile image
    ryanb

    Herb has mentioned several times that extension methods are coming soon in the standard.  One of his recent talks showed off an example of [currently Microsoft only] extension methods.  (I forgot exactly which one that was ... )

    EDIT -- Never mind -- I was thinking of partial classes, not extension methods...

  • User profile image
    Charles

    @ryanb: I'm not sure this is correct, actually, but I'll ask Herb...

    C

  • User profile image
    evildictait​or

    , SteveRichter wrote

    *snip*

    no, I want Microsoft to adhere to standards. But I also want them to provide extensions

    You can't have it both ways. Either Microsoft conforms to standards and doesn't provide extensions, or it provides extensions that don't conform to standards.

  • User profile image
    Bass

    gcc has many extensions to C and C++, and that's fine IMO. You just need to support the standard, and provide a strict standards mode flag (eg: gcc -ansi ...). That way people who need compiler portability can still use the compiler (and verify that they are writing standards compliant code).

     

  • User profile image
    evildictait​or

    , Bass wrote

    You just need to support the standard, and provide a strict standards mode flag 

    Internet Explorer tried that, and they get endless criticism for doing so.

  • User profile image
    SteveRichter

    , evildictaitor wrote

    *snip*

    Internet Explorer tried that, and they get endless criticism for doing so.

    Users can run code compiled by a standards non compliant compiler. They can't use a web site that does not conform to their browser.

     

  • User profile image
    Bass

    I don't think Microsoft C++ doesn't extend standard C++ anyway. What about C++/CLI? Also you should add extensions in such a way that it doesn't break standard compliant code. The point is if I write standard compliant code, it should work in standard compliant browser/compiler. Not that I can't write standard compliant code.

    IE does support web standards really well these days, while still supporting IE-only extensions. That's fine.

  • User profile image
    BitFlipper

    I still think the way extensions in C# was implemented is wrong. It looks and feels like they could not really come up with an ideal syntax and ended up with the awkward result we have today. My suggestion is to do it this way:

    public static extension class string
    {
        /// <summary>
        /// Shows how a method is implemented
        /// </summary>
        public int GetWordCount()
        {
             var wordCount = 0;
             // Code that counts words
             return wordCount;
        }
    
        /// <summary> 
        /// Shows how a property is implemented
        ///</summary>
        public int WordCount
        {
            get 
            { 
                 var wordCount = 0; 
                 // Code that counts words
                 return wordCount;
            }
        }
    }

    Other than the "extension" keyword, it is identical to how the "partial" keyword works. Of course the extension keyword will cause the compiler to impose extension-specific rules. For instance, you can't add fields to the extension class. Doing so will result in a compiler error.

    In addition, the compiler will handle the "this" pointer automatically, and a NullReferenceException will be thrown if you try to access an extension method/property when the object is null. So if I type...

    string nullString = null;
    var wordCount = nullString.WordCount;

    ...I will get a NullReferenceException thrown just like I would if I called any other method on it.

    The advantage is that we don't have to deal with the current awkward syntax that itself can't handle properties. I remember reading somewhere that supposedly the reason that extensions do not support properties is because they could not come up with satisfactory syntax to do it.

    BTW this also allows you to create static methods.

  • User profile image
    evildictait​or

    , Bass wrote

    What about C++/CLI? 

    That's not C++.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.