0

g++ 11.3.0 can't compile this code:

template <typename T>
class Foo {
public:
    Foo<T>() {}
};

Error is

expected unqualified-id before ‘)’ token

One way to fix it is correcting Foo<T> to Foo because modern versions of g++ can understand template class methods inside the class without a template parameter.

But code like this could be compiled in earlier versions of g++.

Manual code fixing is an inconvenient way because some open source dependencies like websocketcpp contain plenty of code like this, it's very inconvenient to make your own forks and fix all code.

So, I suggest there is a flag or some pragma in g++ which may help to work around this problem. Is there some work around here without code fixing?

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
JohnIv
  • 21
  • 1
  • `Foo()` is ill-formed. Replace it with `Foo()`. See [dupe](https://stackoverflow.com/questions/71972000/is-having-a-declaration-stackt-for-the-default-ctor-valid-inside-a-class-te). – Jason Apr 08 '23 at 06:31
  • I think your only option is probably to either not compile with c++20 or to submit an issue (or even better a pull request) to the relevant projects to get them to fix their code – Alan Birtles Apr 08 '23 at 07:11
  • 1
    The flag is `-std` with any C++ version before C++20 as argument. This is an intentional backwards-compatibility breaking change and noted as such in the C++20 standard, see https://timsong-cpp.github.io/cppwp/n4868/diff.cpp17.class#2 for the post-C++20 draft. "_because moders versions of g++ can understand template class methods inside the class without a template parameter._": It was always possible to do it that way in standard C++. So only something pre-standard could be an issue with modified code, but then you wouldn't need to compile for C++20 anyway. – user17732522 Apr 08 '23 at 07:18
  • No option to disable this was added when it was implemented in gcc https://gcc.gnu.org/git/?p=gcc.git;a=blobdiff;f=gcc/cp/parser.c;h=a8082d39aca25b51d261fb98f2d1e7e943a6673d;hp=a6a5d975af3edfe8c5cc7ff1b39280c37e463dbc;hb=4b38d56dbac6742b038551a36ec80200313123a1;hpb=38a4db21e12816c674406f33d8bc4d064d4211d7 – Alan Birtles Apr 08 '23 at 07:21
  • I reopened the question, because OP is _not_ asking for how to fix the code, but how to work around the incompatible change. I think that is not covered in the other question. – user17732522 Apr 08 '23 at 07:22
  • This has already been fixed for websocketpp at least https://github.com/zaphoyd/websocketpp/commit/3197a520eb4c1e4754860441918a5930160373eb – Alan Birtles Apr 08 '23 at 07:23

0 Answers0