Re: OT: C++ Template Functions



Am Donnerstag, 27. Dezember 2007 16:42:29 schrieb Erich Dollansky:
Hi,

Heiko Wundram (Beenic) wrote:
The problem is most probably related to the fact that the group of member
functions is only discriminated by return type (i.e., the template
parameter defines the return type), not by parameter (which could be
inferred).

if nothing got changed over the last years, it is not possible to do
this by definition.

When I needed this the last time, I used a enum as an additonal
parameter to achieve this.

I know, it is not perfect, but it leads to what I intended.

It should work here too. I know, it is not the prefect solution.

This is perfectly possible (nowadays? I've been using this for quite some time
now, but not in the "complexity" of the original mail). Take the following
code, for example, which compiles perfectly fine with gcc (and works, of
course):

"""
template <typename T, typename U>
T* newop(U test)
{
return new T(test);
}

int main(int argc, char** argv)
{
int* x = newop<int>(1);
delete x;
}
"""

The condition with template parameter expansion in a function template
parameter list is that those parameters that can be inferred don't have to be
passed, but those that aren't inferrable have to be, which is the case here
with only one parameter, because the return value cannot be inferred. The
second is inferred to be "int".

Anyway, what's bugging me is that if I define Test<V> in test2() to be
Test<int> or any explicit template, the code compiles fine. Only if I use a
template parameter of the function test2() to declare x (i.e., Test<V>) does
the code fail.

Thanks for the reply!

--
Heiko Wundram
Product & Application Development
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: OT: C++ Template Functions
    ... Heiko Wundram wrote: ... template <typename T, typename U> ... It is not a problem outside a class, the problem comes when you want to do this inside a class as you also would define a class with an - in theory - unknown and unlimited number of member functions. ...
    (freebsd-hackers)
  • Re: Implementing a templated "round" function?
    ... At the moment i have two different implementations, ... I did add a second template parameter S for the source type: ... > I could add a specialisation for every combination of S and T, ... template <typename T, typename S> ...
    (comp.lang.cpp)
  • Re: Dealing with ad hominem attacks in comp.programming
    ... template <int N, int D> ... typedef rationaltype; ... template <typename Lhs, typename Rhs> ...
    (comp.programming)
  • expression template help
    ... template < typename LeftOpd, typename Op, typename RightOpd> ...
    (comp.lang.cpp)
  • Template member function cast bug in VC80SP1 (and more...)
    ... writing a small piece of metaprogramming code, we had to face some problems: ... and both MAP_BASE and MAP define 'iterator' as ... we'll omit template parameters from the text, ... template <typename S> ...
    (microsoft.public.vc.language)