Domanda

I have a problem with templatized classes that I made most abstract with this example.

So for a class which has the form

template <typename T, template <typename T> class MyFunctor>
class MyMainClass
{
    MyFunctor<T> myInstance;
public:
    setConfigOfMyFunctor<ConfigClass>(const ConfigClass& cfg); //problem is here. How can I write this?
}

I define FunctorClass which I will use as the second template parameter in MyMainClass

template <typename T>
class FunctorClass
{
public:
    ConfigClass<T> cfg;
    int a;
    int b;
    int c;
}

where my config class is simply

template <Typename T>
class ConfigClass
{
    T cfgval1;
    T cfgval2;
public:
    void setVals(int cfgVal1, ...);
    //.....
}

If I make an object of this class

int main()
{
    typedef double T;
    MyMainClass<T,FunctorClass> classy;
    ConfigClass<T> config;
    config.setVals(1, 2, ...);
    //so I'm looking for something like the following line (taken from first declaration)
    classy.setConfigOfMyFunctor<ConfigClass>(config); //this is supposed to copy the object config to the FunctorClass's object in MyMainClass.
}

So in brief, I want the object config to be copied to MyMainClass<>::FunctorClass<>::cfg

Is that possible?

If you need more information on the problem, please let me know.

Thank you for any efforts.

È stato utile?

Soluzione

You needn't parametrize setConfigOfMyFunctor with any type since you already know type of accepted config - ConfigClass<T>.

So you should be able to do it like that:

template <typename T, template <typename T> class MyFunctor>
class MyMainClass
{
    MyFunctor<T> myInstance;
public:
    template<template <typename T> class Config>
    setConfigOfMyFunctor(const Config<T>& cfg) {
        myInstance.cfg = cfg;
    }
}

(you'll need to add keyword typename somewhere (before Config<T>, most likely) if compiler asks you to)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top