Mixins and overloading

D

Daniel

I was trying to use a set of mixins which declare the functions with
the same names, but take different argument types. However, gcc
complains that the function calls are ambiguous unless I use a "using"
to explicitly bring the calls in to the scope of the outer class. I
presume the cause is something like that the search won't search all
base classes before trying to resolve the call, but I'm not really
clear. Is there an easier workaround than putting a "using" statement
for each base class/function combination? Thanks. Example code below.


#include <iostream>
template <class T>
class Mixin {
public:
void take(T t) {
std::cout << t << std::endl;
}
};

class Master0: public Mixin<int>,
public Mixin<float> {
public:
};

class Master1: public Mixin<int>,
public Mixin<float> {
public:
using Mixin<int>::take;
using Mixin<float>::take;
};

class Master2 {
public:
void take(int t) {
std::cout << t << std::endl;
}
void take(float t) {
std::cout << t << std::endl;
}
};

void driver() {
Master0 m0;
// fails with error about an ambiguous call
m0.take(1);
Master1 m1;
m1.take(1);
Master2 m2;
m2.take(1);
}
 
S

Sunil Varma

Compiler doesn't overload a function that's present in 2 different
classes.
Cause their scope is different, compiler overloads functions within
the same scope.
In this case the function call resolution has to be done by it's class
name, like

m0.Mixin<int>::take(1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top