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 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.

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.


>
> As a nit, the repeated instances of "../header/" in the error message is
> an annoyance.

That looks like a problem with symlinks or your build system, not gcc's fault.

>
> Diagnostic message and code given below.
>
> thanks
> art
>
> ../header/../header/../header/../header/Stack.h: In member function
> ‘std::string Stack<T>::toString()’:
> ../header/../header/../header/../header/Stack.h:184:46: error: expected
> primary-expression before ‘.’ token
>    184 |       str << setw(3) << s.size() << ": " << T.toString();
>        |                                              ^
>
> # include <iomanip>
> # include <sstream>
>
> using namespace std;
> template <class T>
> class Stack {
>     string toString() {
>        stringstream str;
>        str << setw(3) << s.size() << ": " << T.toString();
>        return str.str();
>     };
> };
>




[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