Topic: Explicit Template Instantiation ?s


Author: jzipnick@best.nospam.com (Jay Zipnick)
Date: 1997/06/27
Raw View
I have a template class with non-type parameters, and a static member function:

    template <int numIntBits, int numFracBits>
    struct FixedPoint
        {
        // ....
        static int FloatToFixed(floatType value)
            { /* ... */ }

        };

    typedef FixedPoint<8, 8> FP_8_8;    // USEFUL TYPEDEF

I want to call the static member function, however the following two lines
of code are not equivalent.

    int x = FixedPoint<8, 8>::FloatToFixed(3.14);  // OK (not using typedef)

    int x = FP_8_8::FloatToFixed(3.14);  // didn't compile -- incomplete type

Using the typedef FP_8_8, my compiler complains:
    Error: illegal use of incomplete struct/union/class 'FixedPoint<8,8>'


[Q1]
Since a typedef is just a synonym shouldn't the compiler treat both lines
of code as being identical?


Given that the message says that the type is incomplete I did the following fix:

    template class FixedPoint<8,8>;  // Cause the template to be instantiated
    . . .
    int x = FP_8_8::FloatToFixed(3.14);


[Q2]
With the explicit template instantiation, everything worked fine. But I am
unclear whether *I* needed to do the explicit template instantiation. The
compiler already had the template declaration and saw I needed to call a
static member function of FixedPoint<8,8>, so shouldn't it have done the
instantiation for me? Without the typedef, it will automatically do the
template instantiation.

Section 14.7.1 point 1 states:
"Unless a class template specialization has been explicitly instantiated
(14.7.2) or explicitly specialized (14.7.3), the class template
specialization is implicitly instantiated when the specialization is
referenced in a context that requires a completely-defined object type."

So is this a compiler bug?


[Q3]
A related question: Is this legal?
   template class FP_8_8;  // Explicit template instantiation using typedef?


Thanks,
Jay Zipnick

--
(Remove the .nospam from my e-mail address to reply)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]