Topic: paramlist keyword and new usage of []


Author: "Samee Zahur" <samee.zahur@gmail.com>
Date: Thu, 19 May 2005 15:34:41 CST
Raw View
To start with, my proposal here doesn't quite fit naturally into
existing C++ practices, but its necessity seems to be climbing with
increasing template usage. The concept's simple, a new keyword
paramlist:

Part 1.
-------
template <paramlist L> void wrapperfun(L args)
{
  fun1(args);
  fun2(args,"extra");
}

so that 'wrapperfun(2,3,"hello")' would instantiate

void wrapperfun(int arg1,int arg2,char* str)
{
  fun1(arg1,arg2,str);
  fun2(arg1,arg2,str,"extra");
}

This can do wonders when we are trying to use *all* versions of a
function in a certain wrapped-up way. Currently, all such methods
require explicitly defined yet similarly coded overloads which are hard
to maintain.

Part 2.
-------
Multiple template parameters of type paramlist could be accepted:

template <paramlist L1,paramlist L2>
  void doublewrap(L1 args1,L2 args2)
{
  fun1(args1);
  fun2(args2,"extra");
}

Now's the dirty part: *calling* this kind of a function!

doublewrap([2,3],["hello"]);

[] here defines literals for any type defined with paramlist. Such
usage does not conflict with any existing usage - yet :) The brackets
are optional if no ambiguities arise. Meaning, the wrapperfun in part1
could be called as 'wrapperfun([2,3,"hello"])' with no change in
effect.

Moreover, things like these would easily be usable in the situations
which has prompted so many in this group to come up with proposals like
'super' or 'inherited'...

class derived: public base1, public base2
{
public:
  template <paramlist L1,paramlist L2> derived(L1 args1,L2 args2)
    : base1(args1),base2(args2) {}
};

calls like these doublewrap([],["hello"]) would expand to

fun1();fun2("hello");

Part 3.
-------
If necessary, array-like access could be provided to elements of
'objects' of paramlist types:

cout<<"first param"<<args[0]<<endl;

usage of terms like args[2] could restrict the usage of the function to
cases only where args has been supplied with at least 3 parameters. I
must admit, however, that I myself am not too sure of the utility of
this part of the feature and am not too eager to back it up - unless
someone here can show me nice practical usage. It could be expanded to
making paramtype a full-blown type declaration:

paramlist myparams[int,string,float];

so that myparams become a new type, but then again, I'm including this
for completeness only - I don't see any real utility yet (myparams type
return value being used as arguement to another function call? maybe -
But we usually have structs for that!)

As I understand it, it requires fundamental changes to the core
language - but it can certainly make wrapper codes far more
maintainable and readable. And given the frequency of 'super' or
'parent' proposals, I'd say this could be a useful feature indeed! It's
a broad change and can yield usefulness in
a wide variety of situations

An analysis of pros/cons would be appreciated.



Samee

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]