I had a wrong perception that template function in a class is instantiated only if it’s invoked. See the below simple code:
template<typename T>
struct A
{
T *p;
T& operator * () { return *p; }
};
int main ()
{
A<int> ai; // ok
int i = *ai; // works fine
A<void> av; // compiler complains even "*av" is not called
}
Just while declaring A<void>, compiler errors out as:
error: forming reference to void
I tried to specialize the function for void outside the template as below:
template<>
void A<void>::operator * () {}
But it doesn’t help and gives error as:
error: no member function ‘operator*’ declared in ‘A<void>’
Is there any way to fix this with C++03 ?
What about
Of course it depends what you want
Ato behave in caseTisvoid. You can also specialise the wholeAstruct forvoid, of course.