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

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

 



* Arthur Schwarz <home@xxxxxxxxxxxx> [2023-11-09 21:46]:
> 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.
>
> As a nit, the repeated instances of "../header/" in the error message is an
> annoyance.
>
> 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();

T is of class type. You can't call a member function on a class. If you
want to call a static class function, write

  T::toString();

If you have an instance of type T, then calling toString() here should
work.
The error message doesn't really guide you towards the actual problem
(but it does show the correct location).
But I'm not a GCC developer - no idea how difficult it would be to
improve this.

Best regards,
Thomas

>       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