4 ms·
> instead you get a bunch of errors that there is no "hash" overload Since you get some kind of compile-time diagnostic, then effectively, the type is checked.
by kazinator 9y ago
> instead you get a bunch of errors that there is no "hash" overload
Since you get some kind of compile-time diagnostic, then effectively, the type is checked. Just, within static time, not as early as you'd like, with not as a relevant a diagnostic.
ISO C++ doesn't dictate the wording of diagnostics; a C++ implementation could work backward from that error, or do whatever else, in order to phrase the diagnostic in terms of a problem in the template.
- TheCoelacanth 9y agoC++ templates are only typechecked for types that are actually instantiated. Rust generics are typechecked using the traits that the generic is declared with. That's a pretty big difference for people writing generic/templated functions though not as big of a difference for people using them.
- deleted 9y ago[deleted]
- kazinator 9y agoSure, also, C++ code is also typechecked only for files that you feed to the compiler and include in your image. C++ templates being checked only for instantiated types makes them flexible. E.g. a template that accesses type::foo or instance.foo will work with anything that has a member named foo. That template argument doesn't have to have a declared relationship to any type which has foo, which would be an annoying restriction for the users. Types that aren't instantiated don't exist. They don't correspond to anything in the executable image. Worrying about them is like a recording studio fussing over how much reverb to add to a one-handed clap.
- TheCoelacanth 9y agoIt makes them more flexible in the same way that dynamic typing is more flexible. A type error that doesn't execute doesn't really exist either. It's fine to not worry about types that don't get instantiated if you are the only person ever instantiating a template, but that's obviously not always the case. If my template doesn't typecheck for a type that someone else wrote, I don't find out about it until they send me a bug report. With typechecking before instantiating, I find out about it as soon as I write it.
- kazinator 9y agoExcept it's not dynamic typing; everything that goes into the output image is type checked. > A type error that doesn't execute doesn't really exist either. Nope; that depends on what you mean by "doesn't execute". Provably doesn't execute? As in: it is removable, dead code? Or: doesn't execute because it's not covered by a test case? So that it could execute if a suitable input is found? A template-generated type that is not instantiated doesn't execute because it doesn't exist. It's not in the image. There is no possible input to the program which can exercise it. The code is basically not written. There is no need to diagnose bugs in code that isn't written. Making users derive template arguments from a specific type is just stupidly "blubby". That C++ has typeless template parameters is one of the few sane things about the way it supports generic programming. > With typechecking before instantiating, I find out about it as soon as I write it. With type checking, that same user who would have run into a problem with the "typeless template" will simply find your template useless, and roll their own. In other words, same situation as before. The type error will still be there. If you have a template that is intennded with foo-like arguments, and the user uses them with a bar-like object, that's going to be diagnosed one way or another. If the generic arguments are weak, it will show up as some constraint violation when the template-generate code uses the the bar-like thing as if it were foo-like. If the generic arguments are strong, the user instead gets an error that the bar-like thing is not a suitable argument to the template, which expects a foo-like. It's the same either way; it's all static checking at the same static time and it doesn't change the circumstances under which errors occur. It just changes the amount of discipline needed to use the generic, and the accuracy/granularity of the diagnostic ("this thing is not a foo" versus "this thing here coming from a template parameter can't be used in this way in this piece of template generated code"). It's easier for the user to adjust the bar object to make it sufficiently foo-like for a given template, than to make it completely conform to the foo type.
- TheCoelacanth 9y ago> With type checking, that same user who would have run into a problem with the "typeless template" will simply find your template useless Unless you have a bug in your template that causes it to have a type error for a type that you didn't intend for it to have a type error for.