type wildcards (or something similar) for function parameters

M

Matthias Buelow

Hi,

is there any way to define a function where one or more parameters might
be of any type, since they aren't used in the function?

In particular, I'd need an operator <<() which always throws an error
and doesn't care which type its second argument is, so ideally I'd like
it to be like:

Stream &operator(Stream &s, anytype-here)
{
throw Blobb;
}

Naively, I have tried

Stream &operator(Stream &s, ...)

which doesn't work.

In general, I'd like to be able to define a function f with one or more
parameters which remain of unspecified type, like I could do, for
example, in SML:

fun f x y = 1+x;
val 'a f = fn : int -> 'a -> int

(here the type of the second argument, y, is not specialized and is
represented by the type variable 'a).

Is this possible to achieve in C++?
 
M

Matthias Buelow

I said:
is there any way to define a function where one or more parameters might
be of any type, since they aren't used in the function?

Ah, just ignore it, in this particular case it can be done by simple
function templates (...).
 
M

Matthias Buelow

Pete said:
Apparently "doesn't work" means doesn't satisfy some requirement that
hasn't been mentioned, since the code snippet above seems to do exactly
what was requested. In what way does this not work?

Consider got the following example:

struct Exn {};

struct S {
int x;
};

S &operator <<(S &s, ...)
{
throw Exn();
}

int main()
{
S s;
s << "asdf";
}

Gcc (4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) gives:

t.cc:7: error: 'S& operator<<(S&, ...)' must not have variable number of
arguments


[If you just wanted to point out in an rather anal way that there's a
typo in the snippet, then just ignore the above.]
 
G

gpderetta

Consider got the following example:
[snipped example showing that operator<< must have exactly two parms]

In addition, you can't pass non pod types via '...', so the solution
also fails the requirement that the unused parameter can be of any
type.
 
M

Matthias Buelow

gpderetta said:
In addition, you can't pass non pod types via '...', so the solution
also fails the requirement that the unused parameter can be of any
type.

Well, I'd think it could figure out that in this case, the second one
isn't used and hence not needed and also omit the type circus from that
argument but that's probably asking too much.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top