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. Simple question about templates

Simple question about templates

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasewpf
5 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:

    template class Tokenizer
    {
    typedef typename T::size_type _Mysizt;
    T strText;
    T strFilter;

    \_Mysizt offset;	// offset of the current substring (token)
    \_Mysizt index;	// index to the next separator character, or npos if not found
    \_Mysizt length;	// length of the current substring, or npos if the last one
    
    public:
    Tokenizer(T strText, T strFilter)
    {
        // code removed for readability
    }
    
    T Next(bool bTrim)
    {
        T strToken;
        if (bTrim)
        {
            // trim leading spaces
    

    // but ' ' will not be accepted for a wstring, and L' ' will not be for a string
    offset = strText.find_first_not_of(' ', offset);
    }
    // code removed for readability
    }
    return strToken;
    }

    I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:

            offset = strText.find\_first\_not\_of(" \\t", offset);
    

    [/edit]

    D J 2 Replies Last reply
    0
    • L Lost User

      I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:

      template class Tokenizer
      {
      typedef typename T::size_type _Mysizt;
      T strText;
      T strFilter;

      \_Mysizt offset;	// offset of the current substring (token)
      \_Mysizt index;	// index to the next separator character, or npos if not found
      \_Mysizt length;	// length of the current substring, or npos if the last one
      
      public:
      Tokenizer(T strText, T strFilter)
      {
          // code removed for readability
      }
      
      T Next(bool bTrim)
      {
          T strToken;
          if (bTrim)
          {
              // trim leading spaces
      

      // but ' ' will not be accepted for a wstring, and L' ' will not be for a string
      offset = strText.find_first_not_of(' ', offset);
      }
      // code removed for readability
      }
      return strToken;
      }

      I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:

              offset = strText.find\_first\_not\_of(" \\t", offset);
      

      [/edit]

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      I'm not sure I entirely understand the problem so my suggestion may be as valuable as a $3 bill. Can you use:

      offset = strText.find_first_not_of(0x20, offset);

      Or could you add the "search for" character as part of the constructor?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      L 1 Reply Last reply
      0
      • D David Crow

        I'm not sure I entirely understand the problem so my suggestion may be as valuable as a $3 bill. Can you use:

        offset = strText.find_first_not_of(0x20, offset);

        Or could you add the "search for" character as part of the constructor?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks, that did work, and your suggestion about the constructor I already tried. However, I am still wondering if there is any way to include string or character literals in such a template.

        1 Reply Last reply
        0
        • L Lost User

          I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:

          template class Tokenizer
          {
          typedef typename T::size_type _Mysizt;
          T strText;
          T strFilter;

          \_Mysizt offset;	// offset of the current substring (token)
          \_Mysizt index;	// index to the next separator character, or npos if not found
          \_Mysizt length;	// length of the current substring, or npos if the last one
          
          public:
          Tokenizer(T strText, T strFilter)
          {
              // code removed for readability
          }
          
          T Next(bool bTrim)
          {
              T strToken;
              if (bTrim)
              {
                  // trim leading spaces
          

          // but ' ' will not be accepted for a wstring, and L' ' will not be for a string
          offset = strText.find_first_not_of(' ', offset);
          }
          // code removed for readability
          }
          return strToken;
          }

          I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:

                  offset = strText.find\_first\_not\_of(" \\t", offset);
          

          [/edit]

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Presumably your goal is to use a set of characters rather than one character for your separator. And the classes in use do not provide that functionality. I believe tokenizers iterate through the string character by character, perhaps for efficiency and perhaps to avoid the problem you have. If you want to avoid iteration then perhaps strtok() might solve it.

          L 1 Reply Last reply
          0
          • J jschell

            Presumably your goal is to use a set of characters rather than one character for your separator. And the classes in use do not provide that functionality. I believe tokenizers iterate through the string character by character, perhaps for efficiency and perhaps to avoid the problem you have. If you want to avoid iteration then perhaps strtok() might solve it.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks, that is the conclusion I came to, and I resolved the issue with a slightly different implementation. I was really just trying to learn a bit more about templates, and wondered if there was any provision for automatically converting constant strings between ASCII and Unicode. It's obviously something to be aware of, and avoid.

            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