473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template parameter depending on another one

Hi
I got a headache on this problem, maybe someone can help me here
I want to create a class which can be handled like this

manager<int, list> // possible variant
manager<int, vector> // another possible variant
manager<float, vector> // and so on

I cannot just create / pass a vector<int>. I need the first information
(e.g. int) inside the manager class, because I wrap this type inside
another template (see below)
The problem is - how do I define such a template? My first Idea was
"leave it to the compiler"

template <class tContent, class tContainer> class manager {

void add(anotherBase<tContent>);

protected:
tContainer<anotherBase<tContent> > m_container;
};

which does not work, because "tContainer is not a template".
So my question - is this possible at all? I did not find anything on
that topic in "The C++ Programming Language", and that's a IMHO a bad
sign ...
Maybe someone knows another way of doing such a thing (derivation would
be possible, but ugly in this case).

Arne

Jul 23 '05 #1
4 1521
I think you are not using the keyword typename when using a dependent type
name. If you still have a problem then post again.

Fraser.
Jul 23 '05 #2


Arne Claus wrote:
Hi
I got a headache on this problem, maybe someone can help me here
I want to create a class which can be handled like this

manager<int, list> // possible variant
manager<int, vector> // another possible variant
manager<float, vector> // and so on

I couldn't find a way using templates, so I switched to macros. The
code below compiles on VC 7.1

// manager.h

#include <vector>
#include <list>

#define DECLARE_MANAGER_CLASS(Content, Namespace, Container) \
class manager_##Content##_Namespace##_##Container \
{ \
public: \
void add(Content value) \
{ \
m_container.push_back(value); \
} \
\
protected: \
Namespace::Container <Content> m_container; \
}

#define MANAGER_CLASS(Content, Namespace, Container) \
manager_##Content##_Namespace##_##Container

// we declare the classes we need
DECLARE_MANAGER_CLASS(int, std, vector);
DECLARE_MANAGER_CLASS(float, std, list);
// main.cpp

#include "manager.h"

int main()
{
MANAGER_CLASS(int, std, vector) x;
MANAGER_CLASS(float, std, list) y;

return 0;
}

I don't like the idea of maintaining identical parameter lists in two
places, but at least, if they are mismatched, you'll get a compilation
error.

dan

Jul 23 '05 #3
Arne Claus wrote:
I got a headache on this problem, maybe someone can help me here
I want to create a class which can be handled like this

manager<int, list> // possible variant
manager<int, vector> // another possible variant
manager<float, vector> // and so on

I cannot just create / pass a vector<int>. I need the first information
(e.g. int) inside the manager class, because I wrap this type inside
another template (see below)
The problem is - how do I define such a template? My first Idea was
"leave it to the compiler"

template <class tContent, class tContainer> class manager {

void add(anotherBase<tContent>);

protected:
tContainer<anotherBase<tContent> > m_container;
};

which does not work, because "tContainer is not a template".
So, what prevents you from declaring 'tContainer' a template?
So my question - is this possible at all? I did not find anything on
that topic in "The C++ Programming Language", and that's a IMHO a bad
sign ...
Maybe someone knows another way of doing such a thing (derivation would
be possible, but ugly in this case).

template<class tContent, template<class> tContainer> class manager {
....
};

Now, you won't be able to use 'vector' or 'list' directly because they
can have more than one template argument. You can, however, do a trick
with a special template template wrapper:

template<class T> struct use_vector { typedef std::vector<T> t; };
template<class T> struct use_list { typedef std::list<T> t; };

and now, when you declare your manager, do:

manager<int, use_vector::t> miv;
manager<double, use_list::t> mdl;

V
Jul 23 '05 #4
>> template <class tContent, class tContainer> class manager {
void add(anotherBase<tContent>);

protected:
tContainer<anotherBase<tContent> > m_container;
};

which does not work, because "tContainer is not a template".
So, what prevents you from declaring 'tContainer' a template?

template<class tContent, template<class> tContainer> class manager {


Ah - I already tried (which did not work)

template<class tContent, template<class T> tContainer<T>> class manager

but your attempt somehow makes more sense :)
Now, you won't be able to use 'vector' or 'list' directly because they
can have more than one template argument.


That's no problem, as I don't actually use vector or list but own
classes (wrappers on e.g. vectors) with exactly one parameter - so that
works fine. But nice to know what to do in such a case.

Thank you.
Arne

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
6
by: Marcel Sebbao | last post by:
I have a function that returns {sin(x),x,cos(x)} depending on a parameter k that can take only 3 integer values: -1, 0, 1. I am trying to do this with template specialization: enum curv {...
7
by: ThazKool | last post by:
Is there an elegant/efficient way to use a typedef of a class passed in to a template parameter. I know that a second template parameter can be given, but I was wondering if there is any way to...
3
by: Tommi Mäkitalo | last post by:
Hi, I try to implement a factory for classes. There are 2 kinds of them, which have different constructors. One of them is the base class. The factory gets all needed parameters in his...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
11
by: mathieu | last post by:
Hi there, I don't think I'll be able to describe my issue correctly, so instead I'll just give a pseudo C++ code I am struggling with. Basically I am looking for a 'pure virtual template'...
4
by: abir | last post by:
I am matching a template, and specializing based of a template, rather than a single class. The codes are like, template<template<typename T,typename Alloc = std::allocator<T> class pix{ }; ...
2
by: Kaushal | last post by:
#include <iostream> #include <boost/mpl/vector.hpp> #include <boost/mpl/find.hpp> using namespace std ; using namespace boost ; template <typename numericTypes> struct evalThis
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.