Topic: deduce type from argument in class template.


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/04/01
Raw View
Omry Yadan wrote:

[...]

> now, suppose I want the same type deduction for a template of class
> template <class T>
> class x
> {
>     public:
>     x(T t) {}
> };
>
> if walking on the footprints of the example above :
>
> void g(int i)
> {
>     x<int> X1(i);   // 1
> // or
>     x  X2(i);          // 2
> }
>
> in the first one, we explicitly tell the compiler that the type of the
> template is int, and in the second, I would expect it to deduce the type
> from the argument, just like it would when calling
> just-a-template-of-function.

[...]

How would your imaginary compiler treat the following:

template<class T> struct X
{
  X(T);
};

template<> class X<float>
{
  X(double);
};

void foo()
{
  X x(3.14); // X<float> or X<double>?
}
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1999/03/24
Raw View
The standard does not allow it.  I agree it would be nice, although it would
probably give the implementers quite a headache.  I think this very problem
is probably the justification for make_pair, binder1st, binder2nd, etc.
About the only other thing you can do to improve readability in this case is
typedef the function signatures, of course then choosing a meaningful name
is always a challenge.

Omry Yadan <omry_y@inter.net.il> wrote in message
news:7d56jf$mjc$1@news2.inter.net.il...
> <cut>, the meat of it was; can template parameters for class templates be
deduced?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Yonat Sharon <yonat@usa.net>
Date: 1999/03/25
Raw View
Omry Yadan wrote:
>     x<int> X1(i);   // 1
> // or
>     x  X2(i);          // 2
[...]
> only number 1 compiles, and number two yield a 'invalid use of
template'
> compile error.

The compiler is right. To work around this, write a template function:

template <typaename T> make_x(T t)
{
    return x<T>(t);
}

--
Have fun,
Yonat.

"The nice thing about standards is that
 there are so many of them to choose from."
                -- Andrew S. Tanenbaum



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html
]


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: Edward Diener <eddielee@abraxis.com>
Date: 1999/03/26
Raw View
Omry Yadan wrote:

> now, suppose I want the same type deduction for a template of class
> template <class T>
> class x
> {
>     public:
>     x(T t) {}
> };
>
> if walking on the footprints of the example above :
>
> void g(int i)
> {
>     x<int> X1(i);   // 1
> // or
>     x  X2(i);          // 2
> }
>
> in the first one, we explicitly tell the compiler that the type of the
> template is int, and in the second, I would expect it to deduce the type
> from the argument, just like it would when calling
> just-a-template-of-function.
> unfortunetly, at my compiler anyway (bc++ 5.01), this is not the case.
> only number 1 compiles, and number two yield a 'invalid use of template'
> compile error.

There is no deduction of the type of argument for class templates as there is
for function templates. Your compiler is correct in this situation. However,
the latest version of Borland's compiler, in C++ Builder 4, is much more
compatible with the C++ standard than BC++ 5.01.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: "Omry Yadan" <omry_y@inter.net.il>
Date: 1999/03/23
Raw View
Hia.
suppose I have a template function :

template<class T> swap(T& x,T& y) {/**/};

when I call it, I can do it in two ways :
void f(int x,int y)
{
    swap<int>(x,y);   // 1
// or
    swap(x,y);           // 2
}

obviously, number 2 will be prefered by most people.

now, suppose I want the same type deduction for a template of class
template <class T>
class x
{
    public:
    x(T t) {}
};

if walking on the footprints of the example above :

void g(int i)
{
    x<int> X1(i);   // 1
// or
    x  X2(i);          // 2
}

in the first one, we explicitly tell the compiler that the type of the
template is int, and in the second, I would expect it to deduce the type
from the argument, just like it would when calling
just-a-template-of-function.
unfortunetly, at my compiler anyway (bc++ 5.01), this is not the case.
only number 1 compiles, and number two yield a 'invalid use of template'
compile error.

in this simple example it does not make a big difference, but what if
the
template is built to except pointers to function, or object functions,
or a
mixed version as type parameters?

concider this :

int f1();                // prototypes for functions to be used.
void f2(int);
struct of1{int operator()();};           // prototypes of object
struct of2{void operator()(int);};    // functions to be used.


template <class F1,class F2>               // a template of class that
uses
function or funciton objects.
class myclas
{
    myclass(F1 f1,F2 f2);
    //...
};


              // several uses of the class, several modes and mixed
modes :
myclass<of1,of2>                    mc1(of1(),of2());           // 1
pure
object functions usage.
myclass<int(*)(),void(*)(int)>   mc2(&f1,&f2);               // 2 pure
functions ptr`s usage
myclass<of1,void(*)(int)>        mc3(of1(),&f2);             // 3 mixed
usage

this complex statments could have beed replaced with the much more
readable
:

myclass   mc4(of1(),of2());           // 1 pure object functions usage.
myclass   mc5(&f1,&f2);               // 2 pure functions ptr`s usage
myclass   mc6(of1(),&f2);             // 3 mixed usage


if the compiler will deduce the type from the arguments here also.

what does the standard state about it?

is my compiler is out of date, or is the standard missing this imported
feature?

--
   Omry Yadan.





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Neil Mowbray" <neil.mowbray@tokaiasia.com.hk>
Date: 1999/03/23
Raw View
You might want to read  "The C++ Programming Language" 3rd e
by Stroustrup.  On page 335 he says that template arguments for
classes are never deduced.  Seems this was a designed decision
although there isn't a detailed discussion.  Maybe you can find
a discussion in one of the other C++ "bibles"

Regards, Neil


Omry Yadan wrote in message <7d56jf$mjc$1@news2.inter.net.il>...
>
>Hia.
>suppose I have a template function :
>
>template<class T> swap(T& x,T& y) {/**/};
>
>when I call it, I can do it in two ways :
>void f(int x,int y)
>{
>    swap<int>(x,y);   // 1
>// or
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]