Nontype class template partial specialisation
-
Hi guys, is it possible to define partial specialisation for a class template that contains nontype parameter? I wrote a simple template example and wanted to do a specialisation for
char *
type. I couldn't figure out how to do it, any thoughts? My original template class is below:template<class kind, int stack_size> class stack { private: int count; // Number of items in the stack kind data[stack_size]; // The items themselves public: // Initialise the stack stack (void) { count = 0; // Zero the stack } // Push an item on the stack void push (const kind item); // Pop an item from the stack kind pop (void) { // Stack goes down by one --count; // Then we return the top value return (data[count]); } };
With push() member function separately defined.
template<class kind, int stack_size> inline void stack<kind, stack_size>::push(const kind item) { data[count] = item; ++count; }
Now I tried to specialise the class for char *
template<int stack_size> class stack<char *, stack_size> { private: int count; // Number of items in the stack char *data[stack_size]; // The items themselves public: // Initialise the stack stack (void) { count = 0; // Zero the stack } // Push an item on the stack void push (const char *item); // Pop an item from the stack char *pop (void) { // Stack goes down by one --count; // Then we return the top value return (data[count]); } }; inline void stack<char *, stack_size>::push(const char *item) <---- error { data[count] = strdup(item); ++count; }
The part above wouldn't compile properly. I got a compile error when it hits the push() function definition. It says the stack_size is undeclared identifier. Thanks.
-
Hi guys, is it possible to define partial specialisation for a class template that contains nontype parameter? I wrote a simple template example and wanted to do a specialisation for
char *
type. I couldn't figure out how to do it, any thoughts? My original template class is below:template<class kind, int stack_size> class stack { private: int count; // Number of items in the stack kind data[stack_size]; // The items themselves public: // Initialise the stack stack (void) { count = 0; // Zero the stack } // Push an item on the stack void push (const kind item); // Pop an item from the stack kind pop (void) { // Stack goes down by one --count; // Then we return the top value return (data[count]); } };
With push() member function separately defined.
template<class kind, int stack_size> inline void stack<kind, stack_size>::push(const kind item) { data[count] = item; ++count; }
Now I tried to specialise the class for char *
template<int stack_size> class stack<char *, stack_size> { private: int count; // Number of items in the stack char *data[stack_size]; // The items themselves public: // Initialise the stack stack (void) { count = 0; // Zero the stack } // Push an item on the stack void push (const char *item); // Pop an item from the stack char *pop (void) { // Stack goes down by one --count; // Then we return the top value return (data[count]); } }; inline void stack<char *, stack_size>::push(const char *item) <---- error { data[count] = strdup(item); ++count; }
The part above wouldn't compile properly. I got a compile error when it hits the push() function definition. It says the stack_size is undeclared identifier. Thanks.
-
Thank you, Joaquín In addition. You can see that only
stack::push()
was needed to change for the specialisation. I am wondering if the C++ template standard provides a way to directly make a specialisation towrds member functions, without having to rewrite whole class specialised declaration? Thanks again. -
Thank you, Joaquín In addition. You can see that only
stack::push()
was needed to change for the specialisation. I am wondering if the C++ template standard provides a way to directly make a specialisation towrds member functions, without having to rewrite whole class specialised declaration? Thanks again.No, you can't partially specialize a member function. The language allows you only to fully specialize a member function. For instance, the following is valid:
// no class specialization prior to this
template<>
inline void stack<char*,1024>::push(char* const)
{
}but I'm afraid this is not what you're after, sorry. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!