Explicit instantiation with body?

Just out of curiosity, I have noticed that Clang currently allows the following program:

template T f() { return 13; }

template int f() { return 1; }

It essentially parses the body of the explicit instantiation only to ignore it.
Was this a conscious decision?

GCC 4.6.3 rejects the program with “expected ‘;’ before ‘{’ token”.

Thanks,
– Larisse.

There is close problem: http://llvm.org/bugs/show_bug.cgi?id=15466 - clang accepts incorrect explicit instantiation, but it is about type templates.

Just out of curiosity, I have noticed that Clang currently allows the following program:

template<typename T> T f() { return 13; }
template int f() { return 1; }

It essentially parses the body of the explicit instantiation only to ignore it.
Was this a conscious decision?

No; please file a bug. You cannot define a function in an explicit instantiation.

GCC 4.6.3 rejects the program with "expected ‘;’ before ‘{’ token".

Well, hopefully we can do better than that.

John.

> Just out of curiosity, I have noticed that Clang currently allows the
following program:
>
> template<typename T> T f() { return 13; }
> template int f() { return 1; }
>
> It essentially parses the body of the explicit instantiation only to
ignore it.
> Was this a conscious decision?

I just spoke to Richard Smith about this, and it appears that the current
behavior is a bit different from the way I just described it above.
The declaration for the template instantiation is parsed, but the
'template' keyword is ignored, which leads to two different behaviors for
the calls f() and f<int>().
While f() returns 1, f<int>() returns 13. f() picks up the declaration "int
f() { return 1; }" while f<int>() picks up the template declaration and
implicitly instantiates it.

No; please file a bug. You cannot define a function in an explicit
instantiation.

I can quickly fix this or submit a patch. Should I still file a bug?

> GCC 4.6.3 rejects the program with "expected ‘;’ before ‘{’ token".

Well, hopefully we can do better than that.

What do you have in mind?

Fixing it is better. :slight_smile:

“error: cannot implement a function in an explicit instantiation”

or something along those lines.

John.

My suggestion was:
* If the declarator-id is not a template-id, issue a "function cannot
be defined in an explicit instantiation" diagnostic and recover by
ignoring the 'template' keyword
* If the declarator-id is a template-id, issue an "explicit
specialization requires 'template<>'" diagnostic with a fixit to add
the <>, and recover as if it were an explicit specialization

Ah, yes, I wasn't thinking about the different recovery paths.
Good point.

John.

Problem solved. r184577.
I hope I got everything right.
Thanks,
– Larisse.

With this fix diagnostics in some cases look unclear. For instance, in these declarations:

template struct pr15466a;
template struct pr15466a { int a; };

the second is obviously an instantiation with template arguments missing. Compiler messages however may be confusing:

t2.cpp:2:26: error: class cannot be defined in an explicit instantiation; if this declaration is meant to be a class definition,
remove the ‘template’ keyword
template struct pr15466a { int a; };

t2.cpp:2:17: error: redefinition of 'pr15466a' as different kind of symbol
template struct pr15466a { int a; };
^
t2.cpp:1:27: note: previous definition is here
template <class T> struct pr15466a;
^

As pr15466a in this example is already known as a template, maybe a message like "missing argumet list" is more appropriate?

Thanks,
--Serge

With this fix diagnostics in some cases look unclear. For instance, in these
declarations:

template <class T> struct pr15466a;
template struct pr15466a { int a; };

the second is obviously an instantiation with template arguments missing.

I think you mean, it's obviously meant to be a definition of the
primary template.

Compiler messages however may be confusing:

t2.cpp:2:26: error: class cannot be defined in an explicit instantiation;
if this declaration is meant to be a class definition,
      remove the 'template' keyword
template struct pr15466a { int a; };
~~~~~~~~~ ^
t2.cpp:2:17: error: redefinition of 'pr15466a' as different kind of symbol
template struct pr15466a { int a; };
                ^
t2.cpp:1:27: note: previous definition is here
template <class T> struct pr15466a;
                          ^

As pr15466a in this example is already known as a template, maybe a message
like "missing argumet list" is more appropriate?

That makes sense to me, but I think it's somewhat orthogonal to this
bug fix. We should also produce a "missing template parameter list"
diagnostic for a case like:

  template <class T> struct pr15466b;
  struct pr15466b { int a; };

> With this fix diagnostics in some cases look unclear. For instance, in
these
> declarations:
>
> template <class T> struct pr15466a;
> template struct pr15466a { int a; };
>
> the second is obviously an instantiation with template arguments missing.

I think you mean, it's obviously meant to be a definition of the
primary template.

> Compiler messages however may be confusing:
>
> t2.cpp:2:26: error: class cannot be defined in an explicit
instantiation;
> if this declaration is meant to be a class definition,
> remove the 'template' keyword
> template struct pr15466a { int a; };
> ~~~~~~~~~ ^
> t2.cpp:2:17: error: redefinition of 'pr15466a' as different kind of
symbol
> template struct pr15466a { int a; };
> ^
> t2.cpp:1:27: note: previous definition is here
> template <class T> struct pr15466a;
> ^
>
> As pr15466a in this example is already known as a template, maybe a
message
> like "missing argumet list" is more appropriate?

That makes sense to me, but I think it's somewhat orthogonal to this
bug fix. We should also produce a "missing template parameter list"
diagnostic for a case like:

  template <class T> struct pr15466b;
  struct pr15466b { int a; };

Interesting. The general logic of the bug fix is that if a declaration
starts as if a template instantiation, but provides a body, then it is
likely meant to be either a non-template definition or a template
specialization.
We decided to select one of these two alternatives based on whether
template arguments are explicitly provided.

It sounds like you are suggesting to also consider the fact that the
existing template may not have been defined as an alternative, in which
case we should treat cases lacking explicit arguments as mis-entered
template definitions (?).

In this latter case, with our recovering from failure by removing the
template keyword (if no argument is provided), the issue becomes the same
as the case that Richard brings up (without the 'template' keyword).

The only adjustment that this bug fix could do for the case you presented
is to not produce the "cannot be defined in an explicit instantiation"
diagnosis when the existing template does not have a definition.

In other words, would you prefer the following diagnosis to the current one
(above)?

t2.cpp:2:17: error: redefinition of 'pr15466a' as different kind of symbol
template struct pr15466a { int a; };
                ^
t2.cpp:1:27: note: previous definition is here
template <class T> struct pr15466a;
                          ^

If so, I can quickly update the bug fix appropriately. If not, then the
issue is probably whether and how the "redefinition" diagnosis should be
issued...

Thanks,
-- Larisse.

As Richard pointed out, I used wrong wording, so let me give long explanation, just to be understood correctly.

The most common kind of error is typos. The next is something omitted, these errors are typical for experienced programmers (“brain is faster than fingers”). Misplaced and extra things are much rarer, the latter are more often for novices. So if we have wrong piece of code, we should try to recover assuming that something is missed. The original case:

template struct pr15466a { int a; };

can be recovered by:

  • Adding parameter list to ‘template’, making it a template definition, or
  • Removal of ‘template’ turning it into non-template class definition.
    According the above rules, missed things are more probable, than spurious ‘template’. People are lazy, if a user typed ‘template’, there must a template somewhere. So this code should be recovered as template definition.

Now let’s consider the next example:

template struct pr15466a;
template struct pr15466a { int a; };

In this case we know that ‘pr15466a’ is a template, so almost definitely the second line must be recovered as a specialization by putting ‘<>’ after ‘template’ and template parameters after ‘pr15466’. Chances that a user made two different errors (typed extra ‘template’ and made a typo in ‘pr15466’) are pretty low.

Bottom line: in both cases I would complain about missed template arguments.

Another problem with your fix is too long messages. Short messages (no more that 5-6 significant words) are perceived as a whole, longer require reading which is substantially (several times) slower and annoy users. The message
class cannot be defined in an explicit instantiation; if this declaration is meant to be a class definition, remove the ‘template’ keyword
I would make something like:
class cannot be defined in an explicit instantiation; spurious ‘template’?

Thanks,
–Serge

As Richard pointed out, I used wrong wording, so let me give long
explanation, just to be understood correctly.

The most common kind of error is typos. The next is something omitted,
these errors are typical for experienced programmers ("brain is faster than
fingers"). Misplaced and extra things are much rarer, the latter are more
often for novices. So if we have wrong piece of code, we should try to
recover assuming that something is missed. The original case:

template struct pr15466a { int a; };

can be recovered by:
- Adding parameter list to 'template', making it a template definition, or
- Removal of 'template' turning it into non-template class definition.
According the above rules, missed things are more probable, than spurious
'template'. People are lazy, if a user typed 'template', there must a
template somewhere. So this code should be recovered as template definition.

Now let's consider the next example:

template <class T> struct pr15466a;
template struct pr15466a { int a; };

In this case we know that 'pr15466a' is a template, so almost definitely
the second line must be recovered as a specialization by putting '<>' after
'template' and

template parameters after 'pr15466'.

Not to be too picky, but I think you meant "template arguments" here? :slight_smile:

Chances that a user made two different errors (typed extra 'template' and
made a typo in 'pr15466') are pretty low.

Bottom line: in both cases I would complain about missed template
arguments.

I believe there have been long and substantial discussions before this bug
fix about the proper way to recover from errors. This fix does not address
what is the proper way, but rather applies what seems to be the currently
agreed-upon rules. Therefore, I'd say that the issue you raise here is
orthogonal to this bug fix.

Another problem with your fix is too long messages. Short messages (no
more that 5-6 significant words) are perceived as a whole, longer require
reading which is substantially (several times) slower and annoy users. The
message
class cannot be defined in an explicit instantiation; if this
declaration is meant to be a class definition, remove the 'template' keyword
I would make something like:
class cannot be defined in an explicit instantiation; spurious
'template'?

You may be right, but this bug fix did not make this message. So, I'd
suggest filing a separate bug report addressing the length of error
messages.

I hope this helps.
Thanks,
-- Larisse.