1

Godbolt link: https://godbolt.org/z/qeqqfMKa6

This code:

#include <string> 

template <typename Tag>
struct StrongSymbol {
  explicit StrongSymbol<Tag>(std::string name) : m_name(std::move(name)) {}
  StrongSymbol<Tag>() : StrongSymbol<Tag>("") {}

  std::string m_name;
};

Builds cleanly in clang, and in gcc fails with:

error: expected unqualified-id before ')' token
    6 |   StrongSymbol<Tag>() : StrongSymbol<Tag>("") {}
      |                     ^

but only if compiled with -std=c++20 (in gcc).
Any idea what happens here? Is this a gcc bug?

Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
  • The program [compiles](https://godbolt.org/z/W7Pseens8) with gcc with c++17. See Dupe: [Template class compiles with C++17 but not with C++20 if unused function can't be compiled](https://stackoverflow.com/questions/72375794/template-class-compiles-with-c17-but-not-with-c20-if-unused-function-cant-b/72376365#72376365) for the reason. – Jason Sep 05 '22 at 11:04
  • @JasonLiam whats wrong in OPs code? Why is it a duplicate? – 463035818_is_not_an_ai Sep 05 '22 at 11:06
  • @463035818_is_not_a_number The program compiles with C++17. See [demo](https://godbolt.org/z/W7Pseens8). Compilers have the freedom to diagnose it or not. – Jason Sep 05 '22 at 11:07
  • @JasonLiam diagnose what? I dont see what in OPs code prevents instantiation. In the question you link the instantiation would be ill-formed. Why here? – 463035818_is_not_an_ai Sep 05 '22 at 11:07
  • @463035818_is_not_a_number Just try instantiating the template and all compiler will give you the error mentioned. But without any instantiation there is no diagnostic required. – Jason Sep 05 '22 at 11:08
  • @JasonLiam no only gcc complains : https://godbolt.org/z/rbEfheTKv – 463035818_is_not_an_ai Sep 05 '22 at 11:10
  • 2
    @463035818_is_not_a_number The problem is that with c++20 we can't write: `StrongSymbol` in constructor declaration. There are plenty of dupes for this. See this dupe: [Is having a declaration Stack(); for the default ctor valid inside a class template](https://stackoverflow.com/questions/71972000/is-having-a-declaration-stackt-for-the-default-ctor-valid-inside-a-class-te) – Jason Sep 05 '22 at 11:10
  • Removing `` would compile with both compilers and seems cleaner anyway. – Jarod42 Sep 05 '22 at 11:13
  • @Jarod42 Yes, with c++20 we have to remove `` to make it well-formed. – Jason Sep 05 '22 at 11:14
  • 1
    Just for anyone reading this in the future: The program is IFNDR. That is, compilers are free to issue a diagnostic or not. See [this](https://stackoverflow.com/questions/72375794/template-class-compiles-with-c17-but-not-with-c20-if-unused-function-cant-b/72376365#72376365) for example. On the other hand, if you were to try instantiating the template say by trying to create an object of type `StrongSymbol`, all compilers then must issue a diagnostic. – Jason Sep 05 '22 at 11:21
  • @JasonLiam Cool. I didn't know this. Re: _"A simple-template-id is no longer valid as the declarator-id of a constructor or destructor."_ - Are `explicit` constructors an exemption to the rule (since those still compile even with gcc)? I couldn't find anything about that in the linked passages of the standard so I assume they haven't implemented diagnostics for it yet? – Ted Lyngmo Sep 05 '22 at 11:24
  • @TedLyngmo *"Are explicit constructors an exemption to the rule (since those still compile even with gcc)...?"* No, they are not exempt from this rule. That is, the rule applies to explicit ctors as well. Note that all compilers behave the same whether or not explicit is used in the example. For example, refer to [without explicit](https://godbolt.org/z/qoq6xcWco) and [with explicit](https://godbolt.org/z/cxzjd9hns) – Jason Sep 05 '22 at 11:31
  • @JasonLiam Adding [`explicit`](https://godbolt.org/z/jT96P1cnz) to the default ctor in the example makes gcc let it through (just as it lets the first converting ctor through) - that's why I asked, but ok, it's not supposed to let it through. Good to know. – Ted Lyngmo Sep 05 '22 at 11:34

0 Answers0