Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Nontype class template partial specialisation

Nontype class template partial specialisation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structureshelpquestiondiscussion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • J J B 0

      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.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The correct syntax for the specialized member of stack::push is as follows:

      template<int stack_size>
      inline void stack<char*,stack_size>::push(const char* item)
      {
      ...
      }

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

      J 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        The correct syntax for the specialized member of stack::push is as follows:

        template<int stack_size>
        inline void stack<char*,stack_size>::push(const char* item)
        {
        ...
        }

        Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

        J Offline
        J Offline
        J B 0
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • J J B 0

          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.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          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[^]!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups