C++ template template vererbung

striker159

Lt. Junior Grade
Registriert
Dez. 2008
Beiträge
327
Hallo,
ich habe eine kleines Problem in meinem C++ Code, wahrscheinlich im Zusammenhang mit Vererbung und Templates.

Ausgangspunkt ist dieses minimal working example:
Code:
#include <iostream>

struct Foo;

template<typename O>
struct GenericOp{
	const Foo& a;
	const Foo& b;

	template<typename T>
	void compute(T target) const{
		((const O*)this)->compute(target);
	}
};

struct CustomOp : public GenericOp<CustomOp>{
	const double factor;

	CustomOp(const Foo& a_, const double f):
		GenericOp<CustomOp>{a_, a_}, factor(f){}


	template<typename T>
	void compute(T target) const;
};

struct Foo{
    double x;
    double y;
    double z;
    
    struct Wrapper{
        Foo* foo;
    };
    
    template<typename Op>
    Foo& assign(const Op& op){
        op.compute(Wrapper{this});
        return *this;
    }
    
    
    friend std::ostream& operator<<(std::ostream& os, const Foo& f){
        os << "[" << f.x << ", " << f.y << ", " << f.z << "]";
        return os;
    }
};

template<typename T>
void CustomOp::compute(T target) const{
    const double tmp = a.x / 2;
    target.foo->x = a.y;
    target.foo->y = tmp * factor;
    target.foo->z = tmp + a.z;
}

int main(){
    
    Foo f1{1.0, 2.0, 3.0};
    
    CustomOp op{f1, 42.0};
    
    Foo f2;
    
    f2.assign(op);
    
    std::cout << f1 << std::endl;
    std::cout << f2 << std::endl;   
}


Nun möchte ich einen Templateparameter zu Foo hinzufügen, was in diesem Code resultiert.

Code:
#include <iostream>

template<int I>
struct Foo;

template<int I, typename O>
struct GenericOp{
	const Foo<I>& a;
	const Foo<I>& b;

	template<typename T>
	void compute(T target) const{
		((const O*)this)->compute(target);
	}
};

template<int I>
struct CustomOp : public GenericOp<I, CustomOp<I>>{
	const double factor;

	CustomOp(const Foo<I>& a_, const double f):
		GenericOp<I, CustomOp<I>>{a_, a_}, factor(f){}


	template<typename T>
	void compute(T target) const;
};

template<int I>
struct Foo{
    double x;
    double y;
    double z;
    
    struct Wrapper{
        Foo<I>* foo;
    };
    
    template<typename Op>
    Foo<I>& assign(const Op& op){
        op.compute(Wrapper{this});
        return *this;
    }
    
    
    friend std::ostream& operator<<(std::ostream& os, const Foo<I>& f){
        os << I << " [" << f.x << ", " << f.y << ", " << f.z << "]";
        return os;
    }
};

template<int I>
template<typename T>
void CustomOp<I>::compute(T target) const{
    const double tmp = a.x / 2;
    target.foo->x = a.y;
    target.foo->y = tmp * factor;
    target.foo->z = tmp + a.z;
}

int main(){
    
    Foo<1> f1{1.0, 2.0, 3.0};
    
    CustomOp<1> op{f1, 42.0};
    
    Foo<1> f2;
    
    f2.assign(op);
    
    std::cout << f1 << std::endl;
    std::cout << f2 << std::endl;   
}

Dieser Code compiliert leider nicht mehr.

55:24: error: ‘a’ was not declared in this scope

Aus irgendeinem Grund scheint CustomOp nicht mehr a von GenericOp zu erben.

Hat jemand eine Idee?
 
Zuletzt bearbeitet:
it works. thank you.

could you explain further why I need to use 'this' in this case, but not in the first case without the template?
 
Zurück
Oben