Topic: Templates and template specialisation


Author: Charles Gardiner <charles.gardiner@mchp.siemens.de>
Date: 1997/05/10
Raw View
Is it a requirement that a template specialisation must have the same
function prototypes as the corresponding template class, or am I free to
define the specialisation anyway I require. i.e. different function
prototypes, different function names, different data elements etc.

I've looked at chapter 14 of the Draft but haven't really found a
conclusive answer. I would be interested to hear opinions of anyone
interested in the topic.

In the following example, the template class has a function called
"trace" with three parameters in the prototype whereas the
specialisation uses four parameters for the corresponding function.



#include <string>

class Trace_File;

template <class T>
class Signal
{
public:
    inline Signal() {}
    // Trace this signal onto the file 'f'.
    void trace(Trace_File* f, const std::string& name, int spaces = 1);
};

// Specialisation for unsigned signals
class Signal<unsigned>
{
public:
    inline Signal() {}
    // Trace the signal attached to this port onto the file 'f'.
    inline void trace(Trace_File* f, const std::string& name, int
width,                int spaces = 1);
};

    // Implementation of inlines for templates
template <class T> void Signal<T>::trace(Trace_File* f, const
std::string& name, int spaces)
{ }

    // Implementation of inlines for specialised class Signal<unsigned>
void Signal<unsigned>::trace(Trace_File* f, const std::string& name,
                             int width, int spaces)
{ }

//        Main Program
//////////////////////////////////////////////////
main() {
    Signal<int>      DataBus;
    Signal<unsigned> Address;
    Trace_File*  OutFile;

         // These two lines should compile ok
    DataBus.trace(OutFile, "Asdf", 6);
    Address.trace(OutFile, "Asdf", 6, 6);

         //  The following two lines should give compile Errors if
commments
         //  are removed
    // DataBus.trace(OutFile, "Asdf", 6, 6);
    // Address.trace(OutFile, "Asdf", 6);
}

/////////////////////////////

Regards,

Charles Gardiner
Siemens AG
charles.gardiner@mchp.siemens.de
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/05/11
Raw View
Charles Gardiner wrote:
>
> Is it a requirement that a template specialisation must have the same
                            ^ class
> function prototypes as the corresponding template class, or am I free
> to define the specialisation anyway I require. i.e. different function
> prototypes, different function names, different data elements etc.

Yes, you're free to do so. (I'm quite sure this is explained in
chapter 14; probably in [temp.expl.spec]).

[...]
> // Specialisation for unsigned signals
> class Signal<unsigned>
> {
[...]

FWIW, the correct syntax for this is now:

 template<> // <- required by CD2
 class Signal<unsigned> { // ...

Many compilers will accept the old-style syntax (the one
you're using) for non-member templates.

 Daveed
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]