0

I'm extending a base template class that provides a template method (that I cannot change). I can instantiate the class, but I cannot use the instatiation of the template method. The compiler is not getting at all what I'm trying to do, and when I try to compile I get syntax error about the template part. Do you know how should I do that? The following code synthesize the same situation.

#include <iostream>

namespace MyNameSpace
{
template<class T, class U>
class BaseTemplateClass
{
    public:
    explicit BaseTemplateClass(T t, U u): paramT{t}, paramU{u}
    {
        std::cout << "BaseConstructor" << std::endl;
    }

    template<typename Z>
    Z Get()
    {
        std::cout << "Call to Get()" << std::endl;
        return Z{};
    }

    private:
    T paramT;
    U paramU;
};

}

template<class V, class W>
class DerivedTemplateClass : public MyNameSpace::BaseTemplateClass<V, double>
{
    using Base = MyNameSpace::BaseTemplateClass<V, double>;
    public:
    DerivedTemplateClass(V t, double u, W w): Base(t, u), paramW{w}
    {
        std::cout << "DerivedConstructor" << std::endl;
    }

    void DoSomething()
    {
        int something = Base::Get<int>();
    }

    private:
    W paramW;
};


using DerivedClass = DerivedTemplateClass<int, bool>;
DerivedClass instance{3, 4.2, true};

To complicated things more, I would also have to instantiate a variable like that: typename V::InternalType something = Base::Get<V>();

Thanks for the help :)

error: expected primary-expression before 'int'

int something = Base::Get(); ^~~ myfile.h:197:35: error: expected ',' or ';' before 'int'

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Conte
  • 1
  • tl;dr of the dupe: `int something = Base::template Get();` – 463035818_is_not_an_ai Feb 22 '23 at 12:24
  • btw, its a class template and method template (not template class and not a template method) – 463035818_is_not_an_ai Feb 22 '23 at 12:25
  • 1
    @463035818_is_not_a_number I always thought "class template" and "template class" are interchangeable. Now, I found how I came to this disbelief: [What is the difference between a template class and a class template?](https://stackoverflow.com/a/39681575/7478597). ;-) – Scheff's Cat Feb 22 '23 at 12:27
  • 1
    @Scheff'sCat I remember someone repeating a good example, but not what it was exactly. Something with chocolate. Salt chocolate is salty chocolate, chocoalte salt is chocalaty salt, complete different things. A class template is a tempalte it is not a class – 463035818_is_not_an_ai Feb 22 '23 at 12:29
  • 1
    @Scheff'sCat I have most respect for the creator of my favourite language, but I dont have to agree with all (he seems to be a fan of `using namespace std;` eg, which is ok, but not my thing) – 463035818_is_not_an_ai Feb 22 '23 at 12:31

0 Answers0