Re: Can you use a function reference passed in a template argument?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 9 Nov 2023 at 21:58, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
>
> On Thu, 9 Nov 2023 at 21:24, Arthur Schwarz <home@xxxxxxxxxxxx> wrote:
> >
> >
> > On 11/9/2023 1:11 PM, Jonathan Wakely via Gcc-help wrote:
> > > On Thu, 9 Nov 2023 at 20:46, Arthur Schwarz <home@xxxxxxxxxxxx> wrote:
> > >>
> > >> Is there any way to use a function passed as an argument to a template
> > >> (example below)? Couldn't the existence of the referenced function be
> > >> established durint instantiation (Stack<some class> obj)? I realize that
> > >> just doing analysis of the template that the existence of a referenced
> > >> function can't be determined, but during instantiation it can be validated.
> > > I have no idea what that code is trying to do. You're trying to call a
> > > member function on a _type_ T.
> > As if I knew what I was trying to do.
> > >
> > > And you said you want to call a function passed as an argument ... but
> > > the template argument you pass is a type, not a function.
> > >
> > At the time of object instantiation the 'type' is a class and as a
> > member of this
> > class there is a function. It is resolvable that when a class is used
> > that to satisfy
> > the requirement that the template object is correct, the class must
> > contain the
> > indicated function. If a passed class does not contain the indicated
> > function,
> > then an error can be generated.
>
> No, you have a category error. You call a non-static member function
> on an object, not on a type.
>
> You could write T().toString() which would create a temporary object
> of type T, and call the function on _that_. But you can't call a
> non-static member function on a type. You need an object.
>
> >
> > It looks like the determination of template instantiability is made when
> > the
> > template is 'compiled', and at this time it is not possible to determine
> > that
> > the referenced function, T.fun(), is available.
>
> T.fun() is not even valid C++ syntax, it's just nonsense. So the
> problem has nothing to do with when the template is instantiated or
> compiled. You're just writing something that isn't C++.


To see that this is not about template instantiation, consider:

struct S {
  string toString() { return "S"; }
};

int main()
{
  std::cout << S.toString();
}

This is the same as what you tried to write, it uses a type S where an
object is required. It's not valid C++.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux