C++ Logo


Advanced search

Re: Fwd: No need for typename and template once it's stated in the concept

From: Christof Meerwald <cmeerw_at_[hidden]>
Date: Fri, 7 Jan 2022 12:12:02 +0100
On Fri, Jan 07, 2022 at 11:02:05AM +0200, Михаил Найденов via Std-Proposals wrote:
> On Thu, Jan 6, 2022 at 7:58 PM Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
> wrote:
> > I would guess not, because remember `typename` and `template` are hints to
> > the *parser* — nothing to do with semantic analysis. So the parser knows
> > that the token sequence
> > template < stuff_as_members T >
> > is introducing a constrained template, but it's not going to go analyze
> > `concept stuff_as_members` to find out what its requirements *are*.

Well, but for non-dependent names you do lookup during parsing to
determine if "<" should be parsed as the start of a template argument
list or as a "less-than" operator. The difference here is that "T" is
still a dependent name and we don't have any special rules for
constrained dependent names to make use of any additional information.

> That looks like an area of improvement to me - if we can use the concept
> beyond syntactical checks to improve the code, we should do so.
> For example
> template<stuff_as_members T>
> void func()
> {
> T::type += 1;
> }

An implementation is already allowed (but not required) to diagnose
this at template definition time, see

  "The validity of a template may be checked prior to any

   The program is ill-formed, no diagnostic required, if:
   (6.1) no valid specialization can be generated for a template ..."

This is no different to any ill-formed non-dependent expressions in
template definitions - it's just that most compilers nowadays already
diagnose at template definition time, but there is still no
requirement to do so in the standard (historically, compilers used to
use token-pasting for template instantiations and couldn't diagnose
anything within template definitions). E.g.

  template<int I>
  void foo()
    *I = 1; // ill-formed, no diagnostic required


https://cmeerw.org                             sip:cmeerw at cmeerw.org
mailto:cmeerw at cmeerw.org                   xmpp:cmeerw at cmeerw.org

Received on 2022-01-07 05:12:07