2

I have to implement a data structure in multiple different ways, each of which should be compatible with using multiple data types.

I then have to make a class that can test all of these implementations using different data types.

Example:

Testobject obj = Testobject<DatastructType1<int>>();
Testobject obj2 = Testobject<DatastructType2<float>>();

I have tried multiple ways to use the correct syntax for the Testobject class.

template <typename T, typename U>
class Testobject {
    public:
        Testobject();

    private:
        T<U> data;

};

It looks like this now, but instead of this first line I have also tried the following:

template <typename T <typename U>>

template <typename T, template <typename> class U>>

template <template <typename T> class U>

So basically I'm wondering what the right syntax is.

EDIT: my lecturer just simplified the assignment, so this is no longer necessary. However, if anyone has a solution, I'm still curious.

1 Answer 1

2

You have several choices depending of the syntax expected:

template <typename T>
class Testobject {
public:
    Testobject();

private:
    T data;
};

Testobject obj = Testobject<DatastructType1<int>>();
Testobject obj2 = Testobject<DatastructType2<float>>();

or

template <template <typename> class C, typename T>
class Testobject
{
public:
    Testobject();

private:
    C<T> data;
};

Testobject obj = Testobject<DatastructType1, int>();
Testobject obj2 = Testobject<DatastructType2, float>();

A variant of first snippet, with partial specialization:

template <typename T> class Testobject;

template <template <typename> class C, typename T>
class Testobject<C<T>>
{
public:
    Testobject();

private:
    C<T> data;
};

Testobject obj = Testobject<DatastructType1<int>>();
Testobject obj2 = Testobject<DatastructType2<float>>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.