Topic: Partially Instantiated Templates


Author: Will Bene <w.j.bene@larc.nasa.gov>
Date: Wed, 7 Mar 2001 23:03:26 GMT
Raw View
I have a template that is instantiated based on a single
primitive type (template<class T>) that I want to explicitly
instantiate one of the methods for float and double, but
allow the compiler to instantiate the same function for
integer types.  Example follows:

#include <stdio.h>
#include <iostream.h>


template <class T> class A
{
    public:
        A() { stuff = T(5); }
        ~A() { }

        T Calculate(T val)
        {
            return val + stuff;
        }

    private:
        T stuff;
};

float A<float>::Calculate(float val)
{
    return val * stuff;
}

int main(int argc, char *argv[])
{
    A<float>   f;
    A<int>     i;

   cout << "Int =   " << i.Calculate(10) << endl;
   cout << "Float = " << f.Calculate(10.0f) << endl;
}

## output ##
$ main
Int =   15
Float = 50
$

I have used this numerous occasions on Solaris, Irix, and Nt and
it works fine.  However if I compile under Irix with -LANG:std I
get the following error:

$ CC main.cpp -o main -LANG:std
cc-3229 CC: ERROR File = main.cpp, Line = 20
  Specializing function "A<float>::Calculate" requires "template<>" syntax.

  float A<float>::Calculate(float val)
                  ^

So the burning question is whether or not this is this considered standard
C++, or is the compiler telling me the syntax is close but no cigar?  If
it is not part of the standard, I find it odd that it works on three
different platforms.

Will


--

William J. Bene
Science and Technology Corporation
10 Basil Sawyer Drive
Hampton, VA 23666-1393
w.j.bene@larc.nasa.gov
(757) 766-8240

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Thu, 8 Mar 2001 17:12:19 GMT
Raw View
> I have a template that is instantiated based on a single
> primitive type (template<class T>) that I want to explicitly
> instantiate one of the methods for float and double,
Are your sure you want to explicitly instantiate the above specializations
and not to explicitly specialize them? It looks like you are trying to do
the latter, as the declaration in an explicit instantiation is not meant to
be a definition.

> but allow the compiler to instantiate the same function for
> integer types.  Example follows:
>
> #include <stdio.h>
> #include <iostream.h>
>
>
> template <class T> class A
> {
>     public:
>         A() { stuff = T(5); }
>         ~A() { }
>
>         T Calculate(T val)
>         {
>             return val + stuff;
>         }
>
>     private:
>         T stuff;
> };
>
> float A<float>::Calculate(float val)
> {
>     return val * stuff;
> }
The above definition is neither an instantiation nor explicit
specialization, and the compiler on Irix is quite right at rejecting it. If
you take a look at C++ Grammar, you'll see:
explicit-instantiation:
   template   declaration
explicit-specialization:
   template < >   declaration

So a correct explicit specialization could be written like:
template<>
float A<float>::Calculate(float val)
{
    return val * stuff;
}

>
> int main(int argc, char *argv[])
> {
>     A<float>   f;
>     A<int>     i;
>
>    cout << "Int =   " << i.Calculate(10) << endl;
>    cout << "Float = " << f.Calculate(10.0f) << endl;
> }

Cheers,
Andrei Iltchenko.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Thu, 8 Mar 2001 17:12:11 GMT
Raw View
"Will Bene" <w.j.bene@larc.nasa.gov> wrote in message
news:3AA66C8D.75E368F3@larc.nasa.gov...
[SNIP]
> template <class T> class A
> {
>     public:
>         A() { stuff = T(5); }
>         ~A() { }
>
>         T Calculate(T val)
>         {
>             return val + stuff;
>         }
>
>     private:
>         T stuff;
> };
>
> float A<float>::Calculate(float val)
> {
>     return val * stuff;
> }
[SNIP]
> I have used this numerous occasions on Solaris, Irix, and Nt and
> it works fine.  However if I compile under Irix with -LANG:std I
> get the following error:
>
> $ CC main.cpp -o main -LANG:std
> cc-3229 CC: ERROR File = main.cpp, Line = 20
>   Specializing function "A<float>::Calculate" requires "template<>"
syntax.
>
>   float A<float>::Calculate(float val)
>                   ^
>
> So the burning question is whether or not this is this considered standard
> C++, or is the compiler telling me the syntax is close but no cigar?  If
> it is not part of the standard, I find it odd that it works on three
> different platforms.
>

The Irix compiler is right - standard C++ requires template<>:

template<> float A<float>::Calculate(float val);

See 14.7.3 of the standard.

I believe template<> is a new thing in the standard, and many compilers
still have pre-standard template support, so it doesn't surprise me that it
is not required in many cases.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]