How to avoid ambiguous overloads on inherited function calls using enable_if or concepts?

55 views Asked by At

I try to implement a property storage using a base class that provides get() and set() functions. The base class is a template and all properties are unique types. This works well as long the property storage is used only once in the inheritance structure. See here

If the set() function is not unique anymore I get the compiler error: set() is ambiguous.

Since the types are unique it would be possible to deactivate the set() function if it is not valid for the template.

Here is my minimal example code (online compiler here):


#include <type_traits>

template<typename Type>
struct Base
{
    Type t;

    template <typename T>
    requires( std::is_same_v<Type,T> )
    void set( T t_ ) {
        t = t_;
    }
};


struct A : public Base<bool>
{

};

struct B : public A, public Base<int>
{

};

int main()
{
    A a;
    a.set(true);

    B b;
    b.set(1); // request for member 'set' is ambiguous
    b.set(false); // should be automatically deligated to A::set()
}

The complete error message from g++-13 is:

main.cc: In function 'int main()':
main.cc:38:11: error: request for member 'set' is ambiguous
   38 |         b.set(1);
      |           ^~~
main.cc:16:14: note: candidates are: 'template<class T>  requires  is_same_v<Type, T> void Base<Type>::set(T) [with Type = int]'
   16 |         void set( T t_ ) {
      |              ^~~
main.cc:16:14: note:                 'template<class T>  requires  is_same_v<Type, T> void Base<Type>::set(T) [with Type = bool]'

Is it possible to solve this somehow?

0

There are 0 answers