mardi 4 octobre 2016

c++ invalid_static cast in accessing mixin of another mixin from host method

consider the following code:

#include <string>
#include <iostream>

class B2 //mixin for BaseSensor
{
    public:
        B2(){}
        ~B2(){}

        std::string printStatistics() const {return std::string("TrendSensor");}
};

class C //mixin for ThresholdSensor
{
    protected:
        C(){}
        ~C(){}

        std::string printSomething( const double & ) {return std::string("CombThreshold");}
};


template<typename ThresholdMixin> //CombThreshold , or others ...
class B1 : protected ThresholdMixin  //mixin for BaseSensor
{
    public:
        B1() : stats(0.0f)  {}
        virtual ~B1(){}

        std::string printStatistics() const { return static_cast<ThresholdMixin*>(this)->printSomething(stats); }
        //std::string printStatistics() const { return ThresholdMixin::printSomething(stats); }
        //std::string printStatistics() const { return this->printSomething(stats); }

    private:
        double stats;
};

template<typename ... SensorType>
class A : public SensorType ... // ThresholdSensor, TrendSensor, EdgeSensor, EnvSensor
{
    public:
        A() {}
        virtual ~A();

        void printAllStatisticss() const
        {
            auto v = { static_cast<SensorType *>(this)->printStatistics()... };
            //auto v = { SensorType::printStatistics()... };
            for (auto s : v)
            {
                std::cout << ": " << s << std::endl;
            }
        }
};


int main(int , char **)
{
    A<B1<C> , B2> cmbs;
    cmbs.printAllStatisticss();
    return 0;
}

The above code does not compile on g++ 4.7.1 with c++11 with error:

g++ -Wall -Wextra -g -std=c++11   -c -o "obj_dbg/mixinHell.opp" "mixinHell.cpp"
mixinHell.cpp: In instantiation of ‘void A<SensorType>::printAllStatisticss() const [with SensorType = B1<C>, B2]’:
mixinHell.cpp:60:27:   required from here
mixinHell.cpp:47:69: error: invalid static_cast from type ‘const A<B1<C>, B2>* const’ to type ‘B1<C>*’
mixinHell.cpp:47:69: error: unable to deduce ‘std::initializer_list<auto>’ from ‘{<expression error>}’
mixinHell.cpp:49:4: error: unable to deduce ‘auto&&’ from ‘v’
mixinHell.cpp:49:4: error: unable to deduce ‘auto’ from ‘<expression error>’
make: *** [obj_dbg/mixinHell.opp] Error 1

I am not sure exactly have to call correctly all the printStatistics() of "A"'s host correctly. Googling about it has also not been very fruitful.

Thank you

Aucun commentaire:

Enregistrer un commentaire