Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any method to use 'delete' more efficiently instead of deleting one-by-one?

I’m trying to make function that gets only int parameters. So when other types(double, char, bool) are included I used delete to exclude that like this code:

#include <iostream>

using namespace std;

int add_int(int x, int y){
    return x + y;
}

int add_int(double, double) = delete;
int add_int(int, double) = delete;
int add_int(double, int) = delete;
int add_int(bool, int) = delete; // need much more code.. very inefficient

int main(void){
    cout << add_int(1, true) << endl; // error should happen
}

But I think this is so inefficient. Is there any method like ‘only int possible’ or ‘exclude double, char, bool(I mean input only one parameter)’?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you can use a function template that deletes everything and then add specializations for the ones you want:

template<class X, class Y>
int add_int(X, Y) = delete;

template<>
int add_int(int x, int y)
{
    return x + y;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading