I encountered a rather unintuitive compiler warning yesterday that I’d thought I’d share. See the simple templated class below:
templateclass ATemplateClass { public: ATemplateClass() { } virtual ~ATemplateClass() { } void Add() { ++size; } void Reset() { size = 0; } private: int size; };
(for some reason my code output tags are doing strange things to liek putting in a and messing up the template tag, grrr!
Obviously this isn’t what my class is doing but is illustrative of the problem. When attempting to use this class on the stack like so:
MeanAveragemyTemplate = MeanAverage ();
I got the compiler warning:
warning: ‘myTemplate ‘ is used uninitialized in this function
After some head scratching (it is clearly being initialised!) one of my colleagues suggested the reason why I was getting the warning. The ‘size’ member of ATemplateClass wasn’t being initialised in the constructor which seems to have the knock on effect of causing the entire template to appear to not be initialised to the compiler! It may be a particularly strict but it fixes the warning nonetheless, hurrah!
