type wildcards (or something similar) for function parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthias Buelow

    type wildcards (or something similar) for function parameters

    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(Strea m &s, anytype-here)
    {
    throw Blobb;
    }

    Naively, I have tried

    Stream &operator(Strea m &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++?
  • Matthias Buelow

    #2
    Re: type wildcards (or something similar) for function parameters

    I wrote:
    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 (...).

    Comment

    • Pete Becker

      #3
      Re: type wildcards (or something similar) for function parameters

      On 2008-08-22 09:14:30 -0400, Matthias Buelow <mkb@incubus.de said:
      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(Strea m &s, anytype-here)
      {
      throw Blobb;
      }
      >
      Naively, I have tried
      >
      Stream &operator(Strea m &s, ...)
      >
      which doesn't work.
      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?

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • gpderetta

        #4
        Re: type wildcards (or something similar) for function parameters

        On Aug 22, 3:33 pm, Matthias Buelow <m...@incubus.d ewrote:
        Pete Becker wrote:
        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:
        >
        [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.

        --
        gpd

        Comment

        • Matthias Buelow

          #5
          Re: type wildcards (or something similar) for function parameters

          gpderetta wrote:
          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.

          Comment

          Working...