Topic: template function arguments


Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/07/11
Raw View
J. Wesley wrote:
>> Could anyone tell me whether the C++ Standard allows for...
>
>> b. classes defined within the scope of a function as template
>> function arguments?
>> again, my compiler(Vc5sp3) says no...

Valentin Bonnard wrote:
> The answer is no.
> Types defined at function scope are called local types
> (they can be classes, unions and enumerations).
> It isn't possible to instantiate a template (class or
> function) with a local type.

Could you explain why in more detail, please?  I'm curious as to
what the implications of doing this would be (perhaps hairy
constraints on implementations that use template repositories?).
(Mind you, I'm not advocating the use of local types with templates,
I just am curious what the technical reasons are.)

The only complication I can think of off the top of my head is
that local class types can't have non-inline member functions
(where would they be defined?) or static member variables (where
would they be defined?), which might impact how templates are
instantiated.

Example:
    void func()
    {
        class Local
        {
        public:
                    Local(): value(0) { }
                    ~Local() { }
            int     val() { return value; }
            void    set(int x) { value = x; }
            ...
        private:
            int     value;
        };

        vector<Local>   vec;    // What problems here?
        ...
    }

Of course, if using a function-local type is a problem, it can
be moved out into file-local scope without too much fuss.

-- David R. Tribble, dtribble@technologist.com --
-- C++, the PL/1 of the 90s.
---
[ 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              ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/07/11
Raw View
David R Tribble <david.tribble@noSPAM.central.beasys.com> writes:

> J. Wesley wrote:
> >> Could anyone tell me whether the C++ Standard allows for...
> >
> >> b. classes defined within the scope of a function as template
> >> function arguments?
> >> again, my compiler(Vc5sp3) says no...
>
> Valentin Bonnard wrote:
> > The answer is no.
> > Types defined at function scope are called local types
> > (they can be classes, unions and enumerations).
> > It isn't possible to instantiate a template (class or
> > function) with a local type.
>
> Could you explain why in more detail, please?

A local type has no linkage. In particular, it doesn't have
a fully qualified name (no foo(int)::my_class).

Handling exported templates would mean that the implementation
has to be able to get inside function scope from the outside.
Programmers can access a subclass a_class::a_sub_class but
there is no equivalent for functions.

I don't know if this restriction is really justified for
non exported templates.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/


[ 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              ]






Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/07/08
Raw View
J. Wesley wrote:
>
> Could anyone tell me whether the C++ Standard allows for...

> b. classes defined within the scope of a function as template function
> arguments?
> again, my compiler(Vc5sp3) says no...

The answer is no.

Types defined at function scope are called local types
(they can be classes, unions and enumerations).

It isn't possible to instanciate a template (class or
function) with a local type.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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              ]





Author: "J. Wesley" <joelreed@no.spam.yahoo.com>
Date: 1998/07/07
Raw View
Could anyone tell me whether the C++ Standard allows for...

a. template functions as template function arguments?
my compiler(Vc5sp3) says no...

b. classes defined within the scope of a function as template function
arguments?
again, my compiler(Vc5sp3) says no...

i'm finding the utility of <algorithm> algorithms lacking a little
because of these restrictions. For example, i have a function "foo"
iterating thru each element in a collection and performing some
operation - so i thought i'd try std::for_each. however the operation
to execute for each element really doesn't seem like it should exist
outside of the function "foo". perhaps i shouldn't be using
std::for_each...?

thanks for any help you might provide!
---
[ 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              ]