public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Klaus Lindemann <kglindemann@yahoo.de>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: Question related to changes in template handling
Date: Sun, 22 May 2022 22:40:18 +0000 (UTC)	[thread overview]
Message-ID: <180050659.1310742.1653259218397@mail.yahoo.com> (raw)
In-Reply-To: <CAH6eHdThGg2M+94S=OD7Rvdy9ePbn5qRt+48q1RJKPTw28eYZQ@mail.gmail.com>

 Thanks, this clears things up (sorry for the botched formatting of the original mail).This also explains why clang handles the code the same way as g++12.
Regards
Klaus
    Am Sonntag, 22. Mai 2022, 20:22:28 MESZ hat Jonathan Wakely <jwakely.gcc@gmail.com> Folgendes geschrieben:  
 
 On Sun, 22 May 2022 at 18:25, Klaus Lindemann via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Hello,
> was the handling of templates changed for g++12? I've run across some programs
> which did compile with e.g. g++8, g++11, but no longer compile with g++12.
> The fix is quite easy, but I wonder if this change is a bug, or a more strict
> implementation of the standard.
> Here is a short example program:================================================// compiles with g++ 11, not g++ 12
> #include <vector>namespace NN  // if namespace is removed (i.e. global namespace is used), problem does not occur{  class OStr {};}
> template <class T> NN::OStr& operator<<(NN::OStr& lhs, const std::vector<T>& rhs) { return lhs << rhs[0]; }
> // moving this definition before the other template definition fixes gcc 12 compile problemtemplate <class T> NN::OStr& operator<< (NN::OStr& lhs, const T& rhs) { return lhs << rhs; }
> int main(){  std::vector<int> a;  NN::OStr os;  os << a;  return 0;}=================================================


Reformatted so it's not illegible:

#include <vector>
namespace NN
// if namespace is removed (i.e. global namespace is used),
// problem does not occur
{
  class OStr {};
}
template <class T>
NN::OStr& operator<<(NN::OStr& lhs, const std::vector<T>& rhs)
{ return lhs << rhs[0]; }
// moving this definition before the other template definition
// fixes gcc 12 compile problem
template <class T>
NN::OStr& operator<< (NN::OStr& lhs, const T& rhs) { return lhs << rhs; }

int main(){
  std::vector<int> a;
  NN::OStr os;
  os << a;
  return 0;
}

GCC 12 is correct. Your code is wrong.

In the expression lhs << rhs[0], name lookup is done for operator<< by
considering the overloads already visible at that point, and overloads
that can be found by ADL in the associated namespaces. The associated
namespaces for OStr and int are NN, and there is no viable overload in
namepace NN.

An operator that is part of the API of NN::OStr should be defined in
namespace NN, not the global namespace. If you define both your
operator<< overloads in the same namespace as class OStr it will work.

N.B. this lookup is consistent for the non-template case, because GCC
12 has been fixed to stop looking in the global namespace when that is
not an associated namespace.
  

      reply	other threads:[~2022-05-22 22:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <786553971.2049625.1653240191948.ref@mail.yahoo.com>
2022-05-22 17:23 ` Klaus Lindemann
2022-05-22 18:23   ` Jonathan Wakely
2022-05-22 22:40     ` Klaus Lindemann [this message]

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=180050659.1310742.1653259218397@mail.yahoo.com \
    --to=kglindemann@yahoo.de \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.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).