forwarding and address

I have a function which is over loaded like this:
1
2
3
void clear(string * var);

void clear(int * var);


In my program I call the function like this:

 
clear(&data_field);


What I would like to do is create a different function that can be called the same way as my clear function which would simply take the paramater, turn around and call the above clear function the same way, but I don't want to have to overload the new function. What type of parameter would my new function have to accept? What would replace the ??? ? Is that even possible?

1
2
3
4
5
6
7
clearx(&data_field);

void clearx(??? var){
   do_something;
   clear(&var);
}


Also is it possible to have an array which could hold data_fields of both string and int? Then walk through the array and call clearx on each of them?
Sounds like a job for templates.
1
2
template <typename T>
void clearx(T var){ /*...*/ }


Also is it possible to have an array which could hold data_fields of both string and int? Then walk through the array and call clearx on each of them?
What do you mean? An array that holds both a string and an int in each element, or an array in which each element can contain either a string or an int? If the former, it can be done with std::pair, or you can make your own struct if you prefer; if the latter, it can be done using generic pointers, but you'll have to keep a second array to encode the type of each element in the first. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
void **array=/*...*/;
bool *types=/*...*/;
array[0]=new int;
types[0]=0;
array[1]=new std::string;
types[1]=1;
//...
for (int a=0;a<n;a++){
    if (!types[a])
        delete (int *)array[a];
    else
        delete (std::string *)array[a];
}

It can also be done using polymorphism.
Last edited on
I'm saying if it's possible to do this:
1
2
3
4
5
6
7

clear(&field);

void clear(int * var);

void clear(string * var);


and have it automatically select the correct version of clear to call. Can I just do this:

1
2
3
4
5
6
7

void array=/*...*/

array[0]=&field

clear(array[0]);


and have it also automatically call the the correct version of clear?
Not without polymorphism.
In order to resolve which overload to call, the compiler needs type information at compile time, and generic pointers carry no type information at all. If you try to do something like that, you'll just get a compiler error because no overload takes a void * as a parameter.
You can do this with boost::variant, however I don't know your level of C++ expertise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include <vector>

struct visitor : boost::static_visitor<void> {
    void operator()( int x ) const {
        std::cout << "Element is an integer = " << x << std::endl;
    }

    void operator()( const std::string& s ) const {
        std::cout << "Element is a string = '" << s << "'" << std::endl;
    }
};

int main() {
    typedef boost::variant< int, std::string > Variant;
    typedef std::vector<Variant>                  VariantList;
    typedef VariantList::const_iterator            CIter;

    VariantList v;

    v.push_back( Variant( 4 ) );
    v.push_back( Variant( std::string( "Hello world" ) ) );

    for( CIter i = v.begin(); i != v.end(); ++i )
       boost::apply_visitor( visitor(), *i );
}

Topic archived. No new replies allowed.