Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template class instantiate without template parameter, automatictype deduction

Reply
Thread Tools

template class instantiate without template parameter, automatictype deduction

 
 
Fei Liu
Guest
Posts: n/a
 
      10-25-2007
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.

template <tpyename T>
void func(T a);

func(0.f);

This will cause func<float> to be instantiated. The user does not have
to explicitly call func<float>(0.f);

However this line of thinking is broken when it comes to a template
class constructor function, e.g.

class A{
int x;
};

template <typename T>
class C{
C(const T & t) : t(t) {}
T t;
};

template <>
class C<A> {
C(const A & t) : t(t) {}
A t;
};


int main(){

A a;
C c(a);
}

The above code can't be successfully compiled. One has to name the type
returned from the constructor call to pick up the object. But what
really distinguishes from normal function call is that even C(a) fails,
compiler comlains missing template argument. The problem is sometimes
you want automatic (auto) type deduction that a compiler can provide but
you can't get it for constructor call.

What's the current best practice to approach such kind of problem, i.e.
automatic type deduction?

Fei
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Template argument deduction and conversion operators. BigMan@abv.bg C++ 3 02-02-2005 09:15 PM
template deduction of array size Gianni Mariani C++ 14 12-04-2003 08:31 PM
Is This Legal Template Code? (Deduction Rules(?)) Chris Johnson C++ 3 08-14-2003 01:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57