Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fold expresion & CRTP

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading