4

我试图实现一个“模板模板模板” - 模板类来满足我的需求(我在使用模板元编程方面很新)。不幸的是,我发现以下主题为时已晚: 模板模板参数

不过,我需要实现如下所列的内容。

根据编译器,最后一个 typedef 不起作用。我不确定,但我认为这是由于 3x 模板限制的限制。在这个简单的例子中是否有可能绕过 3xtemplate 定义?

template < typename TValueType >
class ITTranslator
{
public:
    ITTranslator() = 0;
    virtual ~ITTranslator() = 0;
    virtual void doSomething() = 0;
}

template < typename TValueType >
class TConcreteTranslator1 : public ITTranslator<TValueType>
{
public:
    TConcreteTranslator1(){}
    ~TConcreteTranslator1(){}
    void doSomething() {}
}

template < typename TValueType >
class TConcreteTranslator2 : public ITTranslator<TValueType>
{
public:
    TConcreteTranslator2(){}
    ~TConcreteTranslator2(){}
    void doSomething() {}
}

template < 
    typename TValueType, 
    template < typename TValueType > class TTranslatorValueType 
    >
class ITClassifier
{
public:
    ITClassifier() = 0;
    virtual ~ITClassifier() = 0;
}

template < 
    typename TValueType, 
    template < typename TValueType > class TTranslatorValueType 
    >
class TConcreteClassifier1 : public ITClassifier<TValueType,TTranslatorValueType >
{
public:
    TConcreteClassifier1() {}
    ~TConcreteClassifier1() {}
    void dodo(){}
}


template < 
    typename TValueType,
    template <typename TValueType> class TTranslatorValueType,
    template <template<typename TValueType> class TTranslatorValueType> class TClassifierValueType
 >
class ITAlgorithm
{
public:
    ITAlgorithm()=0;
    virtual ~TAlgorithm()=0;
    virtual run() = 0;
}


template < 
    typename TValueType,
    template <typename TValueType> class TTranslatorValueType,
    template <template<typename TValueType> class TTranslatorValueType> class TClassifierValueType
 >
class TConcreteAlgorithm1 : public ITAlgorithm<TValueType,TTranslatorValueType,TTranslatorValueType>
{
public:
    TConcreteAlgorithm1 (){}
    ~TConcreteAlgorithm1 (){}
    run()
    {
        TClassifierValueType< TTranslatorValueType>* l_classifier_pt = new TClassifierValueType< TTranslatorValueType>( );
        // add this object to a internal list...
    }
}



int main()
{
    typedef TConcreteTranslator1< cvbase::uint32_t > translator_t;
    typedef TConcreteClassifier1< cvbase::uint32_t, TConcreteTranslator1> classifier_t;
    typedef TConcreteAlgorithm1 < cvbase::uint32_t, TConcreteTranslator1, TConcreteClassifier1> algorithm_t; // not possible
    return 0;
}

非常感谢,我真的很感激任何帮助!

编辑:我已经扩展了我的列表(我很确定它不会编译:))以显示我使用奇怪概念的动机:)

4

3 回答 3

2

There is really no need to pass template template parameter around here. Usually you can just take a normal template argument and provide a reasonable default:

template<typename ValueType>
struct translator {};

template<typename ValueType, typename Translator = translator<ValueType>>
struct Classifier {};

template<typename ValueType, 
         typename Translator = translator<ValueType>, 
         typename Classifier = classifier<ValueType, Translator>
         >
struct Algorithm {};

This is done the same way for allocator aware containers.

And please do away with the horrible hungarian-notation prefixes.

NB: It seems from your usage of constructors and destructors that you don't really have a grasp of basic C++. You might want to stay away from templates before you have understood easier concepts.

于 2012-10-17T21:04:36.887 回答
1

是的,可以避免模板模板参数(任何级别)。

模板基本上是一个类型级别的函数。你喂它一种类型,然后得到另一种类型。

模板模板参数本身就是一个类型级函数,接受这种参数的模板是一个高阶类型级函数。

可以使用成员模板实现更高阶的类型级函数,而无需使用模板模板参数。我不确定你的设计是否需要它,但这里有一个快速而肮脏的例子:

// regular type, a.k.a. zeroth-order type-level function, 
// a.k.a. "type of kind *"
struct N
{
    int a;
};

// a first-order type-level function, a.k.a. "type of kind *->*"
// it is wrapped in a regular type
struct B
{
    template <class A> struct Impl
    {
        void foo(A a)
        {
            int aa = a.a;
        }
    };
};

// a second-order type-level function 
// that accepts a (wrapped) first-order type function
// and also a regular type. the kind of it would be (*->*)->*->*
// it applies its first argument to its second argument
struct Z
{
    template <class X, class Y> struct Impl
    {
        typename X::template Impl<Y> ya;
        void bar()
        {
            ya.foo(Y());
        }
    };
};

// now this is something: a third-order type-level function
// that accepts a (wrapped) second-order type-level function
// and a (wrapped) first-order type-level function
// and a zeroth-order type-level function
// it applies its first argument to its second and third arguments
// it is also wrapped in a regular type for consistency
// try to figure out its kind
struct T
{
    template <class P, class Q, class R> struct Impl 
    {
        typename P::template Impl<Q, R> yb;
        void baz()
        {
          yb.bar();
        }
    };
};

T::Impl<Z, B, N> tt;
于 2012-10-17T22:31:28.617 回答
0

在这种情况下,您真的不需要模板参数,基本上唯一的变量类型是 TValueType 对吧?其他类型可以使用 TValueType 在类主体上解析。

像这样的东西:

template < 
    typename TValueType
 >
class TAlgorithm
{
public:
    // TTranslator <TValueType> whatever
    // TTranslatorValueType <TValueType> whatever
    TAlgorithm(){}
    ~TAlgorithm(){}
}
于 2012-10-17T21:01:33.183 回答