By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

template class instantiate without template parameter, automatic type deduction

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
Fei Liu wrote:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
I apologize for the double posting...Wierd seamonkey email/newsreader
client issue.
Oct 25 '07 #2

P: n/a
Fei Liu wrote:
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.
Correct. The syntax involves the _type_ name, not a function name.
The problem is
sometimes you want automatic (auto) type deduction that a compiler can
provide but you can't get it for constructor call.
That's not true at all. In your example it's not the constructor that
is a template for which the argument needs to be deduced, it's the type
template that needs to be instantiated. When constructors are concerned,
this is the correct (and working) example:

struct C {
template<class TC(const T& t) {}
};

struct A {};

int main() {
A a;
C c(a); // compiles just fine
}
What's the current best practice to approach such kind of problem,
i.e. automatic type deduction?
Wrap it in a function template.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 26 '07 #3

P: n/a
Victor Bazarov wrote:
Fei Liu wrote:
>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.

Correct. The syntax involves the _type_ name, not a function name.
>The problem is
sometimes you want automatic (auto) type deduction that a compiler can
provide but you can't get it for constructor call.

That's not true at all. In your example it's not the constructor that
is a template for which the argument needs to be deduced, it's the type
template that needs to be instantiated. When constructors are concerned,
this is the correct (and working) example:

struct C {
template<class TC(const T& t) {}
};
Well you see this is not the complete code I presented intially. You
threw away class member t with your convenient modification. If it needs
to be:
template <typenaem U>
struct C{
template<class TC(const T& t) : t(t) {}
U t;
};
We are back to step 1, once you have a class member whose type depends
on class template parameter.
>
struct A {};

int main() {
A a;
C c(a); // compiles just fine
}
>What's the current best practice to approach such kind of problem,
i.e. automatic type deduction?

Wrap it in a function template.
Indeed I played with it for a bit, but it does not seem to be that
great, for example I could conjure a function like following to simulate
a constructor:
template <typename T, template <typename class RT>
RT<Tcreate(const T& t){
return RT<T>(t);
}

It quickly becomes obvious that RT<Tneeds to be given to instantiate
it and one does not gain anything.

Care to supply an example to illustrate your point?
>
V
Oct 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.