As far as I understand, the following program should not be compiled according to the standard , however the latest versions of gcc , clang and MSVC compile it without errors. Do I misunderstand something or are compiler defects?

12.7 Name resolution [temp.res]

8 ... The program is ill-formed, no diagnostic required, if:

(8.3) every valid specialization of a variadic template requires an empty template parameter pack

 template<typename... Args> struct A { A(const Args&... args) : x(0, args...) {} int x; }; int main() { A<> x; } 
  • And in connection with what exactly should it not be compiled according to you? Well, they called defot constructor, they initialized x 0 . If write A<int> x{0}; - then it doesn't compile, everything is fine - VTT
  • Standard to standard strife. The latest version of C ++ seems to be 17. Therefore, the result is different. I have come across situations when template parameters are derived from function call parameters. It is necessary to look at what standard is used by default. - Adokenai
  • @VTT due to the fact that a template with a variable number of parameters is valid only with an empty package of parameters, or is it not? - travor
  • For variadic template zero is quite a valid number of parameters. - αλεχολυτ
  • one
    @VTT confuses me that this option is prohibited by the standard, judging by the link that I brought. Did you go through it? I correctly understood the meaning of the item? - travor

1 answer 1

The program is ill-formed, no diagnostic required

and

gcc, clang and MSVC compile it without errors

Error - this is diagnostic. In this case, the standard allows it not to print.

(Any error is a diagnostic, but not any diagnostic is an error. More details below.)


In general, the standard does not prohibit compilers from compiling ill-formed programs.

The only requirement is that the compiler is obliged to issue a diagnostic — that is, indicate to the user an error (if there is no 'no diagnostic required' postscript, of course) - using a compilation error or warning.

If the compiler continued to compile despite an error, then the resulting program can behave as it pleases - the standard does not make any guarantees about this.

  • That is, "give diagnostic " = compilation error, or maybe a warning or something else? - travor 2:58
  • @travor Any message. - HolyBlackCat
  • 2
    Only here it is not clear why it is suddenly that if a template with a variable number of parameters can have valid specializations only with, say, 3 parameters, this is normal. And when only with 0 parameters - ill-formed, no diagnostic required . - VTT
  • found a similar question on enSO , there already refuses to compile - travor
  • @VTT, here is DR: 1231 , the reason is not explained there, but it is clear who the instigator is. You can ask him. - ixSci