Home > database >  Fold expresion & CRTP
Fold expresion & CRTP

Time:08-20

I'm writing this small application trying to test about fold expressions and i can't get it to compile, it complaints about ambiguous request on method Play, i don't understand why the signature of the function Play should be different on both calls..

#include <iostream>
#include <any>

using namespace std;

struct A
{
   void play() const
   {
       std::cout << "A..." << std::endl;
   }
};

struct B
{
   void play() const
   {
       std::cout << "B..." << std::endl;
   }
};

template<typename TKey>
struct BaseValue
{
    void Play(const TKey& arg)
    {
        arg.play();
    }
};

template<typename... Keys>
struct MyMap : public BaseValue<Keys>...
{
    
};

int main()
{
    MyMap<A, B> oMyMap;
    
    A a;
    oMyMap.Play(a)

    return 0;
}

CodePudding user response:

Beside the constness mentioned in the comment, the fix is

template<typename... Keys>
struct MyMap : public BaseValue<Keys>...
{
    using BaseValue<Keys>::Play ...;
};

The initial problem, has to do with name-lookup, which happens before overload resolution, see [this answer] https://stackoverflow.com/a/60775944/2412846) and the linked duplicates.

  • Related