473,407 Members | 2,326 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,407 software developers and data experts.

Variadic template functions

5
Is it possible to create a a variadic template function? Something like this:

Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2.     ...
  3.     Foo(int x);
  4.     ...
  5. };
  6.  
  7. class Bar {
  8.     ...
  9.     Bar(int x, double y);
  10.     ...
  11. };
  12.  
  13. template<typename T> class Template {
  14.     ...
  15.     static void method(...) {
  16.         T t(...);  // variadic call
  17.         ...
  18.     }
  19. };
  20.  
  21. int main() {
  22.     Template<Foo>::method(5);
  23.     Template<Bar>::method(5, 0.3);
  24. }
  25.  
I don't want to make Foo and Bar have variadic constructors.
Sep 25 '08 #1
3 1944
dshin
5
I realized my use of "variadic" is a little misleading here - the underlying methods themselves are not variadic.

Basically, I want Template::method() to pass its arguments on to an appropriate constructor. The variadicity comes from the fact that the different constructors do not share the same signature.
Sep 25 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Basically, I want Template::method() to pass its arguments on to an appropriate constructor. The variadicity comes from the fact that the different constructors do not share the same signature.
The two templates are different in their content. Here is where you use the template for, say, all constructors with one argument and then write an explicit specialization(s) for the type(s) that have more than one constrcutor argument. To the user it will appear there is one template. What goes on behind the scenes is largely irrelevant.
Sep 26 '08 #3
dshin
5
The two templates are different in their content. Here is where you use the template for, say, all constructors with one argument and then write an explicit specialization(s) for the type(s) that have more than one constrcutor argument. To the user it will appear there is one template. What goes on behind the scenes is largely irrelevant.
It seems to me then that I'd need the specialized template to rewrite the entire class Template<Bar>, just to add the one extra method, which is not nice.

So that gets me thinking that I'd need to create a SpecializedTemplate<Bar> class to inherit from Template<Bar>, in order to add the extra method() without rewriting the rest of Template<T>.

Is that what you are suggesting?
Sep 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Loony | last post by:
Hi, I want to use variadic functions with stl containers as arguments but I am not sure if that is possible and if so how. I'm using gcc 3.3 on Suse 8.2 Hope someone can help me. Thanx...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
by: Christopher Benson-Manica | last post by:
Do variadic functions (such as printf()) always scan the format string for format specifiers, even when there are no additional arguments? If it is instead a QoI issue, what would be true for a...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Florian G. Pflug | last post by:
Hi I faintly remember that I once stumbled upon a way to declare variadic functions (functions that take a variable number of arguments) in plpgsql - but I just searched the docs and can't find...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
4
by: Boltar | last post by:
Hi Is it possible to nest variadic functions? Eg to do something like this: void mainfunc(char *fmt, ...) { va_list args; va_start(args,fmt);
4
by: Boltar | last post by:
Hi I think I posted a question similar to this on here a while back but I can't find the thread and I need something more general anyway. My problem is I have 2 variadic functions , one of...
2
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.