CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Question C++ Name Mangling

    Why are return types not included in compiler generated mangled names?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ Name Mangling

    I have not studied the topic of how compilers implement name mangling, but my guess is that this is because the return type is not involved in overloading, so the implementer did not see fit to include it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: C++ Name Mangling

    Name mangling is about identifying each unique member function. Since functions within a class can't be overloaded such that they differ only by return type, the return type isn't needed as part of the "key" to differentiating between functions. It would be unnecessary space occupied by the "key" value created by the mangled name.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Join Date
    Jul 2009
    Posts
    3

    Re: C++ Name Mangling

    Thank you both. I guess my question is really why does the language disallow overloading on return type? I understand that we can't overload functions based purely on return type because return type is not part of the mangled name -- but what was the basis for that decision?

  5. #5
    Join Date
    Nov 2006
    Posts
    1,611

    Re: C++ Name Mangling

    I'm sure there are more reasons than occur to me, but here are a few....

    If we had:

    float func();
    double func();
    anyobject func();


    How do we select based on, say....

    func();

    ...it's always reasonably permissible to ignore the return type, it's not required.


    int i = func();

    Obviously there are other similar issues when passing types which require conversion to make a selection, and similar ambiguity is found there, too.


    someotherobject b;

    b = func();

    ...obviously there wouldn't be a selection, but would this always be so?

    What if someotherobject had a copy and assignment to float, or anyobject?


    The mechanism is also illogical. A function is theoretically called before the return is prepared. The logical grouping of these concepts is that of a sequence of steps, not of a unified step, and it isn't reasonable to make the assumption.

    func( b );
    func( otherfunc() );

    The logical grouping of func and b or otherfunc happens at one sequence step. It's true that otherfunc must be called first, and it's return is in the 'position' of b in this logical sequence once that return is completed.

    Imagine the side effects of having to be carful about what version of otherfunc might be called based on the type of the parameter func required.

    This would create a range of complicated, hidden interactions in code that would lead to bugs, but then we can already have such arguments about overloaded functions differentiated by parameter.



    I don't recall reading the final determining reason which made the point otherwise inarguable, such musings are for the PhD's who focus on creating languages. I think, among them...

    void func();
    int func();


    func();

    It would not be possible to select void func and still support the notion of calling a function without regard to the return type.


    int * func();
    int & func();
    int func();

    It's reasonable to allow these different types for parameters to a function, but what about return?

    a = b + func();

    ...reference or value?



    It think, too, one of the guiding principles was to avoid adding features which had reasonable alternatives, so as to avoid mission creep in designing the language, and bloating the complexity of the language (which is unavoidable, though it can be minimized).

    Since selection of values intended for use in the calling code can be obtained through passing via reference, the need for the feature of selection by return type alone is hard to justify in the face of any ambiguities it may impose. The balance of these issues may have been the deciding factor.
    Last edited by JVene; July 19th, 2009 at 03:12 PM.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  6. #6
    Join Date
    Jul 2009
    Posts
    3

    Re: C++ Name Mangling

    Thanks JVene. That makes sense.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured