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. template problem & remove_if

template problem & remove_if

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I have a lot of the following in my code: list.erase (remove_if (list.begin (), list.end (), ToDelete ()), list.end ()); Which just removes entries, if the predicate is met. Trying to be a bit smart, I tried wrapping this up in a nice little template to make it look nicer: template<class Input, class Predicate> void erase_if (Input input, Predicate p) { input.erase (remove_if (input.begin (), input.end (), p), input.end ()); } All looks great, only it doesnt remove any entries and I cant figure out why. It's probably something obvious, so any ideas?

    A 1 Reply Last reply
    0
    • A Anonymous

      I have a lot of the following in my code: list.erase (remove_if (list.begin (), list.end (), ToDelete ()), list.end ()); Which just removes entries, if the predicate is met. Trying to be a bit smart, I tried wrapping this up in a nice little template to make it look nicer: template<class Input, class Predicate> void erase_if (Input input, Predicate p) { input.erase (remove_if (input.begin (), input.end (), p), input.end ()); } All looks great, only it doesnt remove any entries and I cant figure out why. It's probably something obvious, so any ideas?

      A Offline
      A Offline
      antlers
      wrote on last edited by
      #2

      Need to pass input into your function by reference: void erase_if (Input& input, Predicate p) But the compiler might have trouble matching that function signature to a template function (I don't know exactly what the rules are). Perhaps if you wrote the function assuming Input was a pointer type, so you would use input->erase (remove_if (input->begin (), input->end (), p), input->end ()); That way the compiler would be forced to treat it as passed by reference, and you'd be forced to code it that way.

      A 1 Reply Last reply
      0
      • A antlers

        Need to pass input into your function by reference: void erase_if (Input& input, Predicate p) But the compiler might have trouble matching that function signature to a template function (I don't know exactly what the rules are). Perhaps if you wrote the function assuming Input was a pointer type, so you would use input->erase (remove_if (input->begin (), input->end (), p), input->end ()); That way the compiler would be forced to treat it as passed by reference, and you'd be forced to code it that way.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        That did the trick and the compiler had no troubles. I knew it would be something simple, thanks!

        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