is it possible to make function that will accept multiple data types for given argument?

40,664

Solution 1

Your choices are

ALTERNATIVE 1

You can use templates

template <typename T> 
T myfunction( T t )
{
    return t + t;
}

ALTERNATIVE 2

Plain function overloading

bool myfunction(bool b )
{
}

int myfunction(int i )
{
}

You provide a different function for each type of each argument you expect. You can mix it Alternative 1. The compiler will the right one for you.

ALTERNATIVE 3

You can use union

union myunion
{ 
    int i;
    char c;
    bool b;
};

myunion my_function( myunion u ) 
{
}

ALTERNATIVE 4

You can use polymorphism. Might be an overkill for int , char , bool but useful for more complex class types.

class BaseType
{
public:
    virtual BaseType*  myfunction() = 0;
    virtual ~BaseType() {}
};

class IntType : public BaseType
{
    int X;
    BaseType*  myfunction();
};

class BoolType  : public BaseType
{
    bool b;
    BaseType*  myfunction();
};

class CharType : public BaseType
{
    char c;
    BaseType*  myfunction();
};

BaseType*  myfunction(BaseType* b)
{
    //will do the right thing based on the type of b
    return b->myfunction();
}

Solution 2

#include <iostream>

template <typename T>
T f(T arg)
{
    return arg;
}

int main()
{
    std::cout << f(33) << std::endl;
    std::cout << f('a') << std::endl;
    std::cout << f(true) << std::endl;
}

output:

33
a
1

Or you can do:

int i = f(33);
char c = f('a');
bool b = f(true);

Solution 3

Use a template:

template <typename T>
T my_function(T arg) {
  // Do stuff
}

int a = my_function<int>(4);

Or just overload:

int my_function(int a) { ... }
char my_function(char a) { ... }
bool my_function(bool a) { ... }

Solution 4

read this tutorial, it gives some nice examples http://www.cplusplus.com/doc/tutorial/templates/

Share:
40,664

Related videos on Youtube

rsk82
Author by

rsk82

Updated on December 25, 2020

Comments

  • rsk82
    rsk82 over 3 years

    Writing a function I must declare input and output data types like this:

    int my_function (int argument) {}
    

    Is it possible to make such a declaration that my function would accept variable of type int, bool or char, and can output these data types ?

    //non working example
    [int bool char] my_function ([int bool char] argument) {}
    
    • Kashif Khan
      Kashif Khan over 12 years
      you need to look into templates
    • Sergio Tulentsev
      Sergio Tulentsev over 12 years
      it's called templates
    • Mia
      Mia over 6 years
      @SergioTulentsev Your link gives a 403 error
    • Sergio Tulentsev
      Sergio Tulentsev over 6 years
      @pkqxdd: yeah, sometimes links die. Anyhow, you can just google for "c++ templates" and pick one of the first few links. Today it's this.
  • Admin
    Admin over 12 years
    Why not add boost::any, void*, and boost::variant? Might as well go the full way.
  • parapura rajkumar
    parapura rajkumar over 12 years
    @EthanSteinberg Please feel free to edit the answer... I usually like to stick to standard C++
  • Sleepi
    Sleepi over 12 years
    Op asks to return the same type as the argument, not int every time
  • CodeMouse92
    CodeMouse92 over 8 years
    While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • Alberto Schiabel
    Alberto Schiabel about 6 years
    @Rhexis, in this case it implicitly infers the value of the 'typename T' based on what type the argument of the function 'f()' is