Can this be done with templates?

S

sip.address

Hi, I'm newbie with c++ templates and was wondering if this is
achievable.
Consider these classes:

class B {};
class D1 : public B {};
class D2 : public B {};
class D11 : public D1 {};
...etc

Class B will define a couple of simple methods that will be shared (no
need to override them) with its derived classes. With more detail,
class B could be:

class B {
public:
B(const std::string& id) : id_(id) {}
private:
std::string id_;
};

The question is, is there an easy way to generate this tree? I'd like
something like:

GENERATE(D1, B, "D1");
GENERATE(D2, B, "D2");
...etc

so the appropriate class definitions are generated.

Thanks.
 
I

Ian Collins

Hi, I'm newbie with c++ templates and was wondering if this is
achievable.
Consider these classes:

class B {};
class D1 : public B {};
class D2 : public B {};
class D11 : public D1 {};
...etc

Class B will define a couple of simple methods that will be shared (no
need to override them) with its derived classes. With more detail,
class B could be:

class B {
public:
B(const std::string& id) : id_(id) {}
private:
std::string id_;
};

The question is, is there an easy way to generate this tree?

I think you'll have to explain what you are looking to achieve a more
clearly. Would D1 look something like this?

class D1 : public B
{
public:

D1( const std::string& id ) : B(id) {}
};
 
S

sip.address

The question is, is there an easy way to generate this tree?
I think you'll have to explain what you are looking to achieve a more
clearly. Would D1 look something like this?

class D1 : public B
{
public:

D1( const std::string& id ) : B(id) {}

};

Sorry for that. The idea is having this:

class B {
public:
B(const std::string& id) : id_(id) {}
std::string& id() const { return id_; }
private:
std::string id_;
};

class D1 : public B
{
public:
D1(const std::string& id) : B(id) {}
};

class D2 : public B
{
public:
D2(const std::string& id) : B(id) {}
};

class D11 : public D1
{
public:
D11(const std::string& id) : B(id) {}
};

...etc

So given that all classes share functionality, I'd like to generate
them without having to manually define every single class. Dxx don't
define any new method, so everything is inherited from B.

So I'd like:

GENERATE(D1, B, "D1");
GENERATE(D2, B, "D2");

Where the third argument is simply the name of the class passed as
first argument.

Thanks.
 
I

Ian Collins

Sorry for that. The idea is having this:
So given that all classes share functionality, I'd like to generate
them without having to manually define every single class. Dxx don't
define any new method, so everything is inherited from B.

So I'd like:

GENERATE(D1, B, "D1");
GENERATE(D2, B, "D2");

Where the third argument is simply the name of the class passed as
first argument.
OK, that makes a little more sense.

If the derived class don't add any functionality, why have them? If you
want a family of unique classes, how about

template <unsigned N>
class D : public B
{
public:
D(const std::string& id) : B(id) {}
};

template <unsigned N0, unsigned N1>
class DD : public D<N0>
{
public:
D(const std::string& id) : D<N0>(id) {}
};

then you can instantiate D<1>, D<2>, DD<1,1> etc.
 
S

sip.address

OK, that makes a little more sense.
If the derived class don't add any functionality, why have them? If you
want a family of unique classes, how about

template <unsigned N>
class D : public B
{
public:
D(const std::string& id) : B(id) {}

};

template <unsigned N0, unsigned N1>
class DD : public D<N0>
{
public:
D(const std::string& id) : D<N0>(id) {}

};

then you can instantiate D<1>, D<2>, DD<1,1> etc.

Thanks, but I was looking for something more generic. For instance,
this inheritance tree could be deeper (imagine having like 20 classes,
and a tree depth of 5 for instance). Also, I named the classes like
D1, D11, etc. but it was just for brevity. In a normal situation, I'd
use a proper name in each case. This is why I'd like some generator
that also names the classes I want to instantiate, and also defines
its hierarchy.

Would it be possible to achieve this?

I imagine -though it's certainly wrong- something like

template<typename B, typename D>
class D : public B
{
public:
D();
// some other functions..
private:
const std::string string_;
};

And then I'd use GENERATE<Foo, Bar, "Foo"> that would generate the
class definition:

class Foo : public Bar
{
public:
Foo();

private:
const std::string string_;
};

for me.
 
I

Ian Collins

Thanks, but I was looking for something more generic. For instance,
this inheritance tree could be deeper (imagine having like 20 classes,
and a tree depth of 5 for instance). Also, I named the classes like
D1, D11, etc. but it was just for brevity. In a normal situation, I'd
use a proper name in each case. This is why I'd like some generator
that also names the classes I want to instantiate, and also defines
its hierarchy.

Would it be possible to achieve this?

I still can't see why you'd want a tree of derived classes that don't
add functionality to their base.

If you want to be lazy with typing, resort to a macro!
 
S

sip.address

I still can't see why you'd want a tree of derived classes that don't
add functionality to their base.

If you want to be lazy with typing, resort to a macro!

I thought it would be possible to rely on the compiler, for better
type safety.

The rationale of this is just proof of concept, so there's no specific
practical application to it at the moment.
 
K

kwikius

I thought it would be possible to rely on the compiler, for better
type safety.

The rationale of this is just proof of concept, so there's no specific
practical application to it at the moment.-

I don't know whether the following is what you mean:

#include <string>

template <typename Base, int Tag = 0>
struct D : Base{
D(std::string const & id) : Base(id){}
};

struct B{
B(const std::string& id) : id_(id) {}
std::string id_;
};

#include <iostream>
int main()
{
typedef D<B> D1;
typedef D<B,1> D2;
typedef D<D1> D11;
D11 d11("hello") ;
std::cout << d11.id_ <<'\n';
}

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top