From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 13B813858D33 for ; Thu, 9 Nov 2023 21:58:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 13B813858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 13B813858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699567135; cv=none; b=wgfe6ZUeTESVDjosFiQAWQOwV9pZTY03+wX4AUHfgWLReywF0iOa2w52fmoziMBK1TjyC8iVMOSdIkuyubm8mimRNUYWBrEo2R7/bW37NHGwhU3gZ+W9O2IZU8uGPW9R/yDmGTrYHesuqbjxU9HN3w9N94+rjYbs4kHb4sGhKN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699567135; c=relaxed/simple; bh=mQDYoVWhvYsGBWs46Djt1SjYA3B5uLIIAYZ3RP9NBYA=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=UH0TrNKFhZXCLwgbd0D+RZdMDOvnb3zMgD9PiqvQecHiwASbNYreLbOV2IhRm07UO8HO6wW/G+IOP8HXxaazViXDdpMRbZD4qRK+z9+nlEqYe+Gy1DNY7CWY0IZeEgMNqgTx6KauG/2p46tyAhe5ku1kgl/b5hPi/LiiZ4LoyQY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-9dbb3d12aefso241888066b.0 for ; Thu, 09 Nov 2023 13:58:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699567132; x=1700171932; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=YxMct2ca6BtTUJKdk0slX3IpApeUTgwkcwSuGeOvdKs=; b=TGC2YbW6Gu7XP+VHK1En+937Z49//nRb0Cr0ey1EInpJYdWFqkIlB42ki9Y+H2TqJd nkF8O4y4MRXoIvLM2AxSHfz36WsFi3dw8AjhuKcJZjex31BmwDmq2TamNsrTdPw/cBAq /x2RF1vmOFAhlONl+g06pYhZ3HInqsKEDHPBbd68zMeye5bU6BlHAO+EEjPJ2Kvf5qIp DgIFP5TQIoBClVbYY0uYsc3urn9CJdICXXVKve17i/scDYlGdfBScgAPxU3VRpFqC39e dKzBeRIuD3vpQG/0Ru8thkWlr2YVaopx54NNm+LNyRTFQVA/wYq3CejH00+XmId3QYX6 ciVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699567132; x=1700171932; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YxMct2ca6BtTUJKdk0slX3IpApeUTgwkcwSuGeOvdKs=; b=JdqfobAkB3uHavqV+aRB1otor189suOreUrn+KTwIsimP+LAgISpOOMzrLEkQnto4a MMJWfI2p1lq+vGl16Vh43W42d90tE++a+CAoPEAJwBYzHxPPRQnA6WGaTFo0JIHqqN+D ekgRsE2pI1CtscoO5jmXoUH6EzalwkDeu7RwOjShKheh6u+9YzhhmTBgfwhvKTkvxVxY +Ofqoebc0xBdo9TNjH9oEzxlcQNIIfoK8Uj1B9OozwNych2VRmNXM4K+aOvE5+IMV/qK 7PL3aCOavdbdGHgNJyyEPEdP2Ch4d2VNrgfHh6T0T45pK94PuyPu8emM7ql37tuuAR6g bKjQ== X-Gm-Message-State: AOJu0YzncZY2KxFLhiwABh1usjfgktxwtDqcx3eQXhcwI8ktuL9VnqBi AkVT75NAGCyUqGekp7aQuEB5FuBE67XcIvLq+TYZikCt X-Google-Smtp-Source: AGHT+IG1M5gCx5SISmotDVg1FyU/44X9lrsPqStplOODumONz0M3Furk0oqhdkzONjua8ft4j0skwsjtbeEwhLoDy4E= X-Received: by 2002:a17:907:3189:b0:9bf:4915:22ca with SMTP id xe9-20020a170907318900b009bf491522camr5267455ejb.45.1699567132367; Thu, 09 Nov 2023 13:58:52 -0800 (PST) MIME-Version: 1.0 References: <7780aa57-1761-475b-9e09-b05d8867ea04@slipbits.com> In-Reply-To: <7780aa57-1761-475b-9e09-b05d8867ea04@slipbits.com> From: Jonathan Wakely Date: Thu, 9 Nov 2023 21:58:40 +0000 Message-ID: Subject: Re: Can you use a function reference passed in a template argument? To: Arthur Schwarz Cc: gcc-help@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, 9 Nov 2023 at 21:24, Arthur Schwarz wrote: > > > On 11/9/2023 1:11 PM, Jonathan Wakely via Gcc-help wrote: > > On Thu, 9 Nov 2023 at 20:46, Arthur Schwarz 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 obj)? I realize th= at > >> just doing analysis of the template that the existence of a referenced > >> function can't be determined, but during instantiation it can be valid= ated. > > 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++. > This validation only > becomes > possible during template instantiation, that is, when > template_name > is instantiated. To me the question then becomes does the standard requir= e > that checking is not deferred until instantiation. > > >> 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 > >> =E2=80=98std::string Stack::toString()=E2=80=99: > >> ../header/../header/../header/../header/Stack.h:184:46: error: expecte= d > >> primary-expression before =E2=80=98.=E2=80=99 token > >> 184 | str << setw(3) << s.size() << ": " << T.toString(); > >> | ^ > >> > >> # include > >> # include > >> > >> using namespace std; > >> template > >> class Stack { > >> string toString() { > >> stringstream str; > >> str << setw(3) << s.size() << ": " << T.toString(); > >> return str.str(); > >> }; > >> }; > >>