Valid code?

C

chris

I want to implement a class with a variable number of template
parameters, but g++ claims this is not allowed. I believe however I can
emulate multiple parameters with the code below. While this seems to
work I just want to check it is correct, as it seems a little strange (A
give a default parameter, and that then activates a different
specialisation).

#include<stdio.h>
class dummy_class{};
template <class T1,class T2=dummy_class>
struct foo {
foo() {printf("2 parameters\n");}
};

template <class T1>
struct foo<T1,dummy_class> {
foo() {printf("1 parameter\n");}
};

int main(void) {
foo<int> a;
foo<int,int> b;
}
 
V

Victor Bazarov

chris said:
I want to implement a class with a variable number of template
parameters, but g++ claims this is not allowed.

It's not. You can't have two class templates with the same name.
14/5 explicitly prohibits that.
I believe however I can
emulate multiple parameters with the code below. While this seems to
work I just want to check it is correct, as it seems a little strange (A
give a default parameter, and that then activates a different
specialisation).

#include<stdio.h>
class dummy_class{};
template <class T1,class T2=dummy_class>
struct foo {
foo() {printf("2 parameters\n");}
};

template <class T1>
struct foo<T1,dummy_class> {
foo() {printf("1 parameter\n");}
};

int main(void) {
foo<int> a;
foo<int,int> b;
}

To be honest, I don't see anything strange with this. If you didn't
specialise the class with 'dummy_class', then the only way to determine
whether the second argument is 'dummy_class' or not is to have some
kind of internal branching based on some kind of type traits... Ugly.
What you did is more elegant, I believe. However, it still is a bit
incorrect because you can say

foo<int,dummy_class>

and it will say "1 parameter", while I did give both. Perhaps you
could designate the specialisation as "1 and the default" instead...

V
 
C

chris

Victor said:
It's not. You can't have two class templates with the same name.
14/5 explicitly prohibits that.




To be honest, I don't see anything strange with this. If you didn't
specialise the class with 'dummy_class', then the only way to determine
whether the second argument is 'dummy_class' or not is to have some
kind of internal branching based on some kind of type traits... Ugly.
What you did is more elegant, I believe. However, it still is a bit
incorrect because you can say

foo<int,dummy_class>

and it will say "1 parameter", while I did give both. Perhaps you
could designate the specialisation as "1 and the default" instead...

I shall probably just give it a sufficently long name (possibly starting
__ ) and trust people to leave it alone :)

Chris
 
N

Nicolas Pavlidis

chris said:
I want to implement a class with a variable number of template
parameters, but g++ claims this is not allowed. I believe however I
can emulate multiple parameters with the code below. While this seems
to work I just want to check it is correct, as it seems a little
strange (A give a default parameter, and that then activates a
different specialisation).

To have variable parametercounts I'd use Typelists. You can pass such a
typelist to a template and so you have diferent counts of parameters.

Typelists are "invented" by Andrei Alexandrescu (great thanks to him :)
) and are available on [1]. Here a simple example how to use them:

template<typename TheList>
class Foo
{
// working with it
};

typedef TYPE_LIST_2(int, char) MyTypeList;
typedef TYPE_LIST_3(int, char, bool) MySecondTypeList;

int main()
{
Foo<MyTypeList> bar;
Foo<MySecondTypeList> bar_two;
return(0);
}

Inside Foo<..> you have now different counts of types. The instance bar
has tow params and the instanze bar_two has three params.

For more informations see the code or have a look at Modern C++ Design
by the author of the lib.

Kind regards,
Nicolas

[1] http://sourceforge.net/projects/loki-lib/
 
M

Michael Kurz

chris said:
I shall probably just give it a sufficently long name (possibly starting
__ ) and trust people to leave it alone :)

You should not use leading "_" as these are reserved for C++.
 
M

Max Vasin

chris said:
I shall probably just give it a sufficently long name (possibly
starting __ ) and trust people to leave it alone :)
You probably should name it like NullType, NilType, VoidType.
NullType (AFAIK) is used in Andrei Alexandresku book "Modern C++ Design",
so IMHO it will better to adopt this convention.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top