public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thomas Bleher <ThomasBleher@gmx.de>
To: Arthur Schwarz <home@slipbits.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Can you use a function reference passed in a template argument?
Date: Thu, 9 Nov 2023 22:02:52 +0100	[thread overview]
Message-ID: <ZU1I/OYtW/j1q4jS@bluelight> (raw)
In-Reply-To: <f73d9139-dbd8-4628-a237-68c96d352245@slipbits.com>

* Arthur Schwarz <home@slipbits.com> [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();
>    };
> };
>


  reply	other threads:[~2023-11-09 21:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 20:46 Arthur Schwarz
2023-11-09 21:02 ` Thomas Bleher [this message]
2023-11-09 21:07   ` Arthur Schwarz
2023-11-09 21:11 ` Jonathan Wakely
2023-11-09 21:24   ` Arthur Schwarz
2023-11-09 21:58     ` Jonathan Wakely
2023-11-09 22:00       ` Jonathan Wakely
2023-11-09 22:10         ` Arthur Schwarz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZU1I/OYtW/j1q4jS@bluelight \
    --to=thomasbleher@gmx.de \
    --cc=gcc-help@gcc.gnu.org \
    --cc=home@slipbits.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).