How to declare/define a class with template template parameters without using an extra template parameter

Consider the following use of template template parameters...

#include <iostream>

template <typename X>
class A
{
    X _t;
public:
    A(X t)
        :_t(t)
    {
    }
    X GetValue()
    {
        return _t;
    }
};

template <typename T, template <typename T> class C >
class B
{
    C<T> _c;
public:
    B(T t)
        :_c(t)
    {
    }
    T GetValue()
    {
        return _c.GetValue();
    }
};

using namespace std;

int main()
{
    B<int, A> b(10);
    cout<<b.GetValue();
    return 0;
}

Is there a way by which the template parameter T can be removed? For example is there a way to make the following work?

//Does not compile
template <template <typename T> class C >
class B
{
    C _c;
public:
    B(T t)
        :_c(t)
    {
    }
    T GetValue()
    {
        return _c.GetValue();
    }
};

int main()
{
    B< A<int> > b(10);
    cout<<b.GetValue();
    return 0;
}

Answers


I assume you're after X, as well as A, in your code.

The usual pattern is to have

template<typename C>
struct B
{
   C c;
};

and then, inside classes eligible for substitution:

template<typename X>
class A
{
   typedef X type_name;
   X t;
};

Then you can access the template parameter using C::type_name.


Need Your Help

Get yesterday's date

ruby-on-rails ruby ruby-on-rails-3

This may be an obvious question but im new to ruby and I've spent ages trying to get yesterdays date for a cucumber test that checks a url that includes yesterday's date. for example:

How can I convert a swing JTextField component in to an object of a type that extends it

java swing inheritance jtextfield mouse-listeners

I have an application using a third party package that has a factory that returns to me JTextField objects that are then added to a GUI. This makes up about 10% of the JTextFields used.

Appropriate data structure for flat file processing?

data-structures flat-file

Essentially, I have to get a flat file into a database. The flat files come in with the first two characters on each line indicating which type of record it is.