Topic: Overloading on Number of Template Parameters


Author: WSEYMOUR@email.usps.gov
Date: 1997/08/14
Raw View
Is it possible to overload on the number of template parameters?
Is there a better way?  Example:

class api
{
    virtual void exec(const void*, size_t, size_t) = 0;
public:
    template<class T, class U> void exec(const T& arg)
    {
        exec(&arg, sizeof(T), sizeof(U));
    }
    template<class T> void exec(const T& arg)
    {
        exec(&arg, sizeof(T), 0);
    }
};


struct foo_1 { ... };
struct foo_2 { ... };

void foo(api& a, const foo_1& arg)
{
    a.exec<foo_1, foo_2>(arg);
}


struct bar_1_only { ... };

void bar(api& a, const bar_1_only& arg)
{
    a.exec(arg);
}



Thanks,

--Bill
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/08/15
Raw View
WSEYMOUR@email.usps.gov wrote:
>
> Is it possible to overload on the number of template parameters?

Not really.

> Is there a better way?  Example:
>
> class api
> {
>     virtual void exec(const void*, size_t, size_t) = 0;
> public:
>     template<class T, class U> void exec(const T& arg)
>     {
>         exec(&arg, sizeof(T), sizeof(U));
>     }
>     template<class T> void exec(const T& arg)
>     {
>         exec(&arg, sizeof(T), 0);
>     }
> };

Since you cannot deduce the second template argument, why
does it matter that the two `exec' member templates have
identical names?

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: WSEYMOUR@email.usps.gov
Date: 1997/07/26
Raw View
Is it possible to overload on the number of template parameters?
Is there a better way?  Example:

class api
{
    virtual void exec(const void*, size_t, size_t) = 0;
public:
    template<class T, class U> void exec(const T& arg)
    {
        exec(&arg, sizeof(T), sizeof(U));
    }
    template<class T> void exec(const T& arg)
    {
        exec(&arg, sizeof(T), 0);
    }
};


struct foo_1 { ... };
struct foo_2 { ... };

void foo(api& a, const foo_1& arg)
{
    a.exec<foo_2>(arg);
}


struct bar_1_only { ... };

void bar(api& a, const bar_1_only& arg)
{
    a.exec(arg);
}



Thanks,

--Bill
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/07/28
Raw View
WSEYMOUR@email.usps.gov writes:

>Is it possible to overload on the number of template parameters?

I don't think so, although I think you can achieve similar effects
using slightly different means.

>Is there a better way?  Example:
>
>class api
>{
>    virtual void exec(const void*, size_t, size_t) = 0;
>public:
>    template<class T, class U> void exec(const T& arg)
>    {
>        exec(&arg, sizeof(T), sizeof(U));
>    }
>    template<class T> void exec(const T& arg)
>    {
>        exec(&arg, sizeof(T), 0);
>    }
>};

You could write that as something like this
(beware, this is completely untested code):

 class api
 {
     virtual void exec(const void*, size_t, size_t) = 0;
     template <class T>
     class size<T> {
  static size_t get_size() { return sizeof(T); }
     };
     template <>
     class size<no_size> {
  static size_t get_size() { return 0; }
     };
 public:
     class no_size {};
     template<class T, class U = no_size> void exec(const T& arg)
     {
  exec(&arg, sizeof(T), size<U>.get_size());
     }
 };

>struct foo_1 { ... };
>struct foo_2 { ... };
>
>void foo(api& a, const foo_1& arg)
>{
>    a.exec<foo_2>(arg);
>}

This <foo_2> here would be specifying class T, not class U.
I think you need to write

 a.exec<foo_1, foo_2>(arg);

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]