473,396 Members | 1,892 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,396 software developers and data experts.

Valid code?

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;
}
Jul 22 '05 #1
5 1395
chris wrote:
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
Jul 22 '05 #2
Victor Bazarov wrote:
chris wrote:
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...


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

Chris
Jul 22 '05 #3
chris <ca*@cs.york.ac.uk> writes:
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/
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #4

"chris" <ca*@cs.york.ac.uk> schrieb im Newsbeitrag
news:41**************@cs.york.ac.uk...
Victor Bazarov wrote:
chris wrote:

#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...


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++.

Jul 22 '05 #5
chris <ca*@cs.york.ac.uk> writes:
Victor Bazarov wrote:
chris wrote:
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...


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.

--
WBR, Max Vasin.
JID: ma******@jabber.ru
ICQ: 276438891

Jul 22 '05 #6

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

Similar topics

12
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
8
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
1
by: jel | last post by:
I ran what was submitted in several forums, but it's not exactly what i'm looking for. I'm dy'n over here. Ah, the frustrations of an amateur programmer. I included the code below in c++. which...
7
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
64
by: jrefactors | last post by:
Since C is a subset of C++, so any C code or C libraries (printf(), scanf(), etc...) are valid C++ code. Is that correct? so even though the whole program is written in C, but with .cpp...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.