468,753 Members | 1,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,753 developers. It's quick & easy.

template class instantiate without template parameter, automatictype deduction

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<floatto 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
Oct 25 '07 #1
0 1050

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Thomas Heller | last post: by
4 posts views Thread by Mat DeLong | last post: by
8 posts views Thread by Tony Johansson | last post: by
4 posts views Thread by Neelesh | last post: by
2 posts views Thread by coolpint | last post: by
7 posts views Thread by Dilip | last post: by
3 posts views Thread by =?gb2312?B?wfXquw==?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.