From 4843066166963544170
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,83aac58a7c7d6c46
X-Google-Attributes: gidf78e5,public
From: David R Tribble <dtribble@technologist.com>
Subject: Re: Advice: C9X's __func__ on C++
Date: 1999/06/12
Message-ID: <376159BD.7E92050E@technologist.com>#1/1
X-Deja-AN: 488784765
Content-Transfer-Encoding: 7bit
Approved: Valentin Bonnard <bonnard@clipper.ens.fr>
References: <slrn7lra47.6l.bcombee@bcombee.metrowerks.com>
X-Original-Date: Fri, 11 Jun 1999 13:47:25 -0500
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Member of the Vast Right-Wing Conspiracy
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUAN2JqlqwEuYhIxRhxAQFGhgH/dt7AqRTYkSFePqLy/1mO+i44hWxpOSG9 WOJb0gKwqSnsiEEtrD+K+6/kkat3jZl+GgitJ9L1eFPAsIKQ25IMOQ== =hZs0
Mime-Version: 1.0
Newsgroups: comp.std.c++

Ben Combee wrote:
> The drafts of C9X (soon to be C0X, it seems) define a predefined
> identifier called __func__ that is visible in any function as a
> static array of char holding the name of the function.
> 
> I want advice on what the contents of this should be for our C++
> compiler.  C9X defines the name to be the simple name of the function,
> so for a function called foo, it would be "foo".  With overloading
> and namespaces, just using "foo" seems insufficient for the C++
> version of this, but on the other hand, using a namespace-complete 
> form with expanded argument list seems like overkill.
> 
> Any advice?  What would be most useful for the programmers?  Also,
> note that C9X's assert() macro requires the use of __func__ in its
> output.

Speaking as the one who proposed the '__func__' identifier to the
ISO C9X committee, I can tell you what I had in mind for C++.
For C, it suffices to simply use the unadorned function name, since
C doesn't have class scopes or namespaces.  For C++, there are a few
reasonable approaches:

1.  Use the fully adorned function name, which includes the fully
 expanded namespace(s), class prefix(es), template instantiation
 parameters, argument prototypes, etc.

 Consider this example:

    namespace Mine
    {
        class Foo
        {
            template <class E>
            class Bar
            {
                template <class T>
                    int func(int i);
                template <class T>
                    int func(float a, const char *c);
            };
        };
    }

 The 'Mine::Foo::Bar<bool>::func<int>(int)' function above would have
 a '__func__' value like one of the following:

    "Mine::Foo::Bar<bool>::func<int>(int)"
    "Mine:Foo:Bar<bool>:func<int>(int)"
    "Mine/Foo.Bar<b>.func<i>(i)"

 or even perhaps something simpler, just as long as it was readable.

2. Use only the unadorned function name for '__func__', which does
 not include any namespace, class, template, or parameter names.
 But in addition to '__func__', add the following reserved identifiers:

    __namespace__
        Contains the fully expanded namespace prefix for a function
        or "" for global functions.  (E.g., "std::" or perhaps
        something shorter such as "std.".)
        Example above: "Mine::".

    __class__
        Contains the fully expanded class prefix for member
        functions, or "::" for non-member functions.  Includes the
        template parameters, if any, with which the class was
        instantiated (if the class is a template class).
        Example above: "Foo::Bar<bool>::".

    __template__
        Contains a representation of the template parameters with
        which the function was instantiated, otherwise "".
        Example above: "<int>".

    __args__ (or __parms__)
        Contains a representation of the function argument prototype
        (such as "(int, const char*)" or "int, const char *", or
        perhaps even something simpler like "i,Cc*").
        Example above: "(int,const char*)".

 (All of these have type 'const char[N]'.)

 The advantage of having several predefined identifiers is so that
 programmers can pick and choose how much of the function name they
 choose to display in debugging statements.  The assert() macro
 should probably use all of them, though, so that there is no
 ambiguity for the user at runtime as to which function caused the
 assertion to fail.

3. Use the mangled function name for its '__func__' value.

 This guarantees a unique value for each '__func__', but at the
 expense of user readability at runtime.

Keep in mind that there need not be any overhead for '__func__' if
it is not used in a function.  Note also that '__func__' cannot be
a preprocessor macro, since the preprocessing phase knows nothing
about function names or scopes.

You can read my original proposal at
<http://www.flash.net/~dtribble/text/c9xfunc.txt>; as I recall,
I mentioned a few issues about C++ in it.  (Note that the original
name proposed was '__func'.)

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



