How to delete a function in C++11

Share the Article

Traditionally, the purpose of delete keyword was only deallocation of heap memory. However, in C++11, this keyword has improved and it can now ask compiler to disable or hide any definition of a function. This article shall explain different scenarios about how to delete a function in C++11. Also, how a program uses this with polymorphic classes.

Delete a specific overload of an overloaded function

Overloaded functions means two or more functions with same name but different arguments. Such overloads can either be available in a 3rd party library or in a separate source file. The delete keyword here can ask compiler to disable access to one or more of overloads.

In the following example, if there are 2 overloads of a given function getSquareRoot( ). These functions are coming from a different file (mysqrt.cpp). The user of this library can delete one of the overload.

Following is the source file of library.

//mysqrt.cpp #include <cmath> //for sqrt( ) using namespace std; double getSquareRoot(double number) //Overload#1 { return sqrt(number); } double getSquareRoot(int number) //Overload#2 { return sqrt(number); }

Now the file test.cpp wants to use the overloaded function

//test.cpp
#include <iostream> //main header using namespace std; //for namespace double getSquareRoot(int number) //Overload#1 is available double getSquareRoot(int number) = delete; //Overload#2 is deleted int main() { double sqroot1 = getSquareRoot(4.0); double sqroot2 = getSquareRoot(4); //calling deleted overload return 0; }

Invocation with integer argument (like above) will generate compiler error. This is because the integer argument overload no longer exists.

Delete a base class member function in derived class in C++ 11

Firstly, we know that the derived class inherits all the public members from base class. Therefore, a derived class object is able to call all the members in base. This is normal feature of inheritance.

However, it is possible that derived class can disable one or more of base class functions using delete. The following example illustrates this behavior.

#include <iostream> using namespace std; class MFBase { public: void funda1() { cout << "funda1()" << endl; } void funda2() { cout << "funda2()" << endl; } }; class MFDerived : public MFBase { public: void funda2() = delete; }; int main() { MFDerived d1; d1.funda1(); d1.funda2(); //Calling deleted member return 0; }

Output :

Without deleted statement, the same program would have run successfully

class MFDerived : public MFBase { //public: //void funda2() = delete; }; MFDerived d1; d1.funda2(); //Not deleted member now return 0; }

Output:

Delete a specific template specialization

The template is also a form of polymorphism, i.e. static polymorphism.

Here, the delete is very usefull. Just like the overload and inheritance case, once of the template specialization can delete without any problems.

The following example shows how compiler can hide one specialization of template function getSquare( ) .

#include <iostream> //main header using namespace std; //for namespace template<typename T> double getSquare(T rhs) //Template function { return rhs*rhs; } template<> double getSquare(int rhs) = delete; //Specialization with int argument int main() { double sqroot1 = getSquare(4.0); double sqroot2 = getSquare(4); //passing int value return 0; }

The output is:

Main Funda: Using delete, we can disable any function, overload or template specialization.

Related Topics:

 What are the drawbacks of using enum ?
Which member functions are generated by compiler in class?
How to stop compiler from generating special member functions?
Compiler Generated Destructor is always non-virtual
How to make a class object un-copyable?
Why virtual functions should not be called in constructor & destructor ?
Explaining C++ casts
How pointer to class members are different ?
How std::forward( ) works?
Rule of Three
How std::move() function works?
What is reference collapsing?
How delete keyword can be used to filter polymorphism
Smart Pointers: shared_ptr <T> 
Smart Pointers: unique_ptr<T>
Understanding Constant Variables

Share the Article

1,466 thoughts on “How to delete a function in C++11

  1. This is a very good tip particularly to those fresh to the blogosphere. Simple but very precise info… Thanks for sharing this one. A must read post!

  2. I seriously love your site.. Very nice colors & theme. Did you create this amazing site yourself? Please reply back as I’m planning to create my own website and want to know where you got this from or exactly what the theme is named. Kudos.

  3. I’m impressed, I must say. Rarely do I come across a blog that’s equally educative and engaging, and let me tell you, you’ve hit the nail on the head. The issue is an issue that not enough men and women are speaking intelligently about. Now i’m very happy I found this during my hunt for something concerning this.

  4. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you

  5. Next time I read a blog, Hopefully it does not disappoint me just as much as this one. I mean, I know it was my choice to read through, however I really believed you would have something helpful to say. All I hear is a bunch of whining about something you can fix if you weren’t too busy searching for attention.

  6. An impressive share! I’ve just forwarded this onto a coworker who has been doing a little homework on this. And he in fact ordered me breakfast because I found it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this issue here on your web page.

  7. Next time I read a blog, I hope that it does not disappoint me as much as this one. After all, Yes, it was my choice to read, but I really thought you would probably have something interesting to say. All I hear is a bunch of whining about something that you could possibly fix if you were not too busy seeking attention.

  8. I blog quite often and I truly thank you for your content. This great article has truly peaked my interest. I will bookmark your site and keep checking for new information about once per week. I opted in for your RSS feed as well.

  9. Right here is the right site for everyone who hopes to find out about this topic. You realize a whole lot its almost hard to argue with you (not that I personally will need to…HaHa). You definitely put a brand new spin on a subject that’s been discussed for decades. Wonderful stuff, just wonderful.

  10. I absolutely love your website.. Great colors & theme. Did you create this web site yourself? Please reply back as I’m looking to create my own personal website and want to learn where you got this from or just what the theme is named. Thank you!

  11. I truly love your website.. Pleasant colors & theme. Did you create this web site yourself? Please reply back as I’m planning to create my own website and want to know where you got this from or just what the theme is called. Appreciate it!

  12. A motivating discussion is definitely worth comment. There’s no doubt that that you should publish more on this issue, it may not be a taboo matter but generally people don’t talk about these subjects. To the next! Kind regards!

  13. Having read this I thought it was rather informative. I appreciate you spending some time and effort to put this informative article together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worthwhile.

  14. After exploring a handful of the blog posts on your blog, I really appreciate your way of writing a blog. I saved it to my bookmark webpage list and will be checking back soon. Take a look at my web site as well and let me know what you think.

  15. An intriguing discussion is worth comment. There’s no doubt that that you ought to write more on this subject, it may not be a taboo matter but typically people don’t talk about such topics. To the next! All the best.

  16. Hello, I do think your blog could possibly be having web browser compatibility problems. When I look at your blog in Safari, it looks fine but when opening in I.E., it has some overlapping issues. I merely wanted to give you a quick heads up! Aside from that, great blog!

  17. Spot on with this write-up, I truly believe that this site needs much more attention. I’ll probably be returning to read more, thanks for the information.

  18. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I get actually loved account your blog posts. Any way I will be subscribing for your augment and even I success you get admission to persistently fast. !

  19. Your style is really unique in comparison to other folks I’ve read stuff from. I appreciate you for posting when you have the opportunity, Guess I will just bookmark this blog.

  20. Howdy! This post could not be written much better! Reading through this article reminds me of my previous roommate! He always kept preaching about this. I’ll send this article to him. Pretty sure he will have a very good read. I appreciate you for sharing!

  21. When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now every time a comment is added I get 4 emails with the same comment. Perhaps there is a means you are able to remove me from that service? Many thanks.

  22. Everything is very open with a precise clarification of the challenges. It was definitely informative. Your website is very helpful. Thank you for sharing.

  23. I would like to thank you for the efforts you’ve put in penning this site. I am hoping to view the same high-grade content by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own, personal website now 😉

  24. Spot on with this write-up, I actually believe this site needs a lot more attention. I’ll probably be back again to read through more, thanks for the information.

  25. Spot on with this write-up, I honestly think this amazing site needs a lot more attention. I’ll probably be returning to read through more, thanks for the information.

  26. An impressive share! I have just forwarded this onto a co-worker who was doing a little research on this. And he in fact bought me lunch due to the fact that I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending some time to talk about this issue here on your site.

  27. You’re so cool! I do not think I’ve truly read something like this before. So great to find another person with unique thoughts on this issue. Seriously.. thanks for starting this up. This site is one thing that is needed on the web, someone with a little originality.

  28. Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It’s always interesting to read through content from other writers and practice something from other sites.

  29. I seriously love your website.. Great colors & theme. Did you build this site yourself? Please reply back as I’m planning to create my own website and want to know where you got this from or what the theme is named. Kudos!

  30. You’re so awesome! I do not think I’ve truly read a single thing like this before. So wonderful to discover another person with original thoughts on this subject matter. Really.. thanks for starting this up. This site is one thing that is required on the internet, someone with a little originality.

  31. After I originally commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I receive four emails with the same comment. Is there a way you are able to remove me from that service? Kudos.

  32. I blog quite often and I really appreciate your information. Your article has truly peaked my interest. I’m going to book mark your website and keep checking for new information about once per week. I opted in for your Feed as well.

  33. Having read this I believed it was very enlightening. I appreciate you finding the time and effort to put this information together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worthwhile.

  34. The very next time I read a blog, I hope that it doesn’t fail me as much as this one. I mean, Yes, it was my choice to read through, but I actually believed you would probably have something helpful to say. All I hear is a bunch of moaning about something you could possibly fix if you were not too busy seeking attention.

  35. I needed to thank you for this good read!! I certainly enjoyed every little bit of it. I have you saved as a favorite to check out new things you post…

  36. Right here is the perfect blog for anyone who hopes to understand this topic. You realize so much its almost hard to argue with you (not that I really would want to…HaHa). You certainly put a brand new spin on a subject which has been discussed for a long time. Great stuff, just great.

  37. I’m amazed, I must say. Seldom do I encounter a blog that’s both equally educative and amusing, and let me tell you, you have hit the nail on the head. The problem is an issue that too few people are speaking intelligently about. I am very happy that I came across this in my hunt for something relating to this.

  38. A motivating discussion is worth comment. There’s no doubt that that you should write more on this subject, it may not be a taboo subject but usually people don’t talk about such topics. To the next! Many thanks!

  39. After exploring a few of the articles on your site, I truly appreciate your technique of writing a blog. I saved as a favorite it to my bookmark webpage list and will be checking back soon. Please check out my website as well and tell me what you think.

  40. Greetings, I think your web site might be having browser compatibility issues. When I look at your blog in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, excellent site.

  41. I’d like to thank you for the efforts you have put in penning this site. I am hoping to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own, personal blog now 😉

  42. Hello, I believe your blog could be having internet browser compatibility problems. Whenever I look at your web site in Safari, it looks fine but when opening in I.E., it has some overlapping issues. I just wanted to give you a quick heads up! Aside from that, great blog!

  43. Nice post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It’s always helpful to read content from other authors and practice a little something from other websites.

  44. Thanks for your post. One other thing is when you are advertising your property yourself, one of the troubles you need to be mindful of upfront is when to deal with household inspection reviews. As a FSBO vendor, the key to successfully transferring your property as well as saving money with real estate agent commissions is awareness. The more you realize, the more stable your home sales effort will probably be. One area when this is particularly significant is reports. zabawy dla dorosłych

  45. That is a good tip particularly to those fresh to the blogosphere. Short but very precise information… Appreciate your sharing this one. A must read article.

  46. You’re so awesome! I do not think I have read through something like that before. So nice to find someone with some unique thoughts on this subject matter. Really.. many thanks for starting this up. This website is one thing that is needed on the internet, someone with some originality.

  47. Hello! I could have sworn I’ve been to this web site before but after going through many of the articles I realized it’s new to me. Nonetheless, I’m certainly delighted I stumbled upon it and I’ll be bookmarking it and checking back often!

  48. Oh my goodness! Incredible article dude! Thank you so much, However I am having troubles with your RSS. I don’t understand why I am unable to subscribe to it. Is there anyone else getting the same RSS problems? Anyone that knows the answer will you kindly respond? Thanx!!

  49. Hi, I do think this is a great web site. I stumbledupon it 😉 I am going to come back once again since I book-marked it. Money and freedom is the best way to change, may you be rich and continue to help others.

  50. Oh my goodness! Awesome article dude! Thank you so much, However I am encountering issues with your RSS. I don’t know the reason why I cannot join it. Is there anybody else having similar RSS issues? Anybody who knows the answer will you kindly respond? Thanx!!

  51. I must thank you for the efforts you have put in writing this blog. I really hope to see the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has encouraged me to get my very own website now 😉

  52. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  53. Hello there! This is my first visit to your blog! We are a collection of volunteers and
    starting a new initiative in a community in the
    same niche. Your blog provided us beneficial information to work on. You have done a wonderful job!

  54. May I simply say what a relief to uncover someone that genuinely understands what they’re talking about online. You certainly know how to bring an issue to light and make it important. More and more people should read this and understand this side of your story. I was surprised that you aren’t more popular given that you surely have the gift.

  55. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  56. wonderful publish, very informative. I wonder why the other specialists of this sector don’t realize this. You should proceed your writing. I am sure, you’ve a great readers’ base already!

  57. I seriously love your blog.. Great colors & theme. Did you create this site yourself?
    Please reply back as I’m attempting to create my own site
    and want to know where you got this from or just what the theme
    is named. Many thanks!

  58. Hi there would you mind letting me know which web
    host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers and I must
    say this blog loads a lot quicker then most. Can you suggest a good web hosting provider
    at a fair price? Thanks a lot, I appreciate it!

  59. An interesting discussion is definitely worth comment.
    I do think that you should publish more on this subject matter, it may not be a
    taboo matter but typically people do not speak about these subjects.
    To the next! Many thanks!!

  60. Howdy would you mind stating which blog platform you’re working with?
    I’m planning to start my own blog in the near future but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.
    P.S My apologies for getting off-topic but I
    had to ask!

  61. Woah! I’m really digging the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s very difficult to
    get that “perfect balance” between usability and visual appearance.
    I must say you have done a excellent job with this. Also,
    the blog loads super fast for me on Safari. Outstanding Blog!

  62. Do you have a spam problem on this site; I also am a
    blogger, and I was curious about your situation; many of us have developed some nice
    practices and we are looking to trade solutions with others, why not shoot me an e-mail if interested.

  63. hello!,I really like your writing so so much! proportion we keep up a correspondence extra approximately
    your post on AOL? I require an expert in this house to solve my problem.
    Maybe that is you! Taking a look ahead to see you.

  64. You’re so interesting! I do not think I have read anything like that before.
    So nice to find somebody with a few unique thoughts on this topic.
    Seriously.. thank you for starting this up. This
    site is one thing that is required on the web, someone with a bit of originality!

  65. I’m not sure why but this web site is loading incredibly slow for me.
    Is anyone else having this problem or is it a issue
    on my end? I’ll check back later and see if
    the problem still exists.

  66. I like the valuable information you supply for your articles.
    I’ll bookmark your blog and check once more
    here regularly. I’m relatively certain I’ll be told lots of new stuff
    right here! Good luck for the following!

  67. I have learn several just right stuff here. Definitely price bookmarking for revisiting. I surprise how so much effort you set to make this kind of magnificent informative website.

  68. Hi there, I do think your website could be having web browser compatibility issues.
    Whenever I take a look at your web site in Safari, it looks fine however, if
    opening in Internet Explorer, it has some overlapping issues.
    I just wanted to provide you with a quick heads up!

    Other than that, wonderful site!

  69. Currently it seems like Expression Engine is the preferred blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?