473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

template class instantiate without template parameter, automatic type 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
3 3542
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
8
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
4
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40);...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
7
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
3
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I'm running into a question as below: template<typename T> class A { private: T _a; public: A(T t): _a(t) { }
0
by: Fei Liu | last post by:
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...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.