Rakesh's Blog

Default Template Argument.

 

When the user drags an inheritance line to a template or specialization, what would be good defaults for the generated template arguments?

 

Say in code I have...

 

template <class T> List {};

class MyClientList {};

 

The user drags an inheritance line from MyClientList to List<T>. Currently we have set the default template argument to “int” and so the code generated will be…

 

template <class T> List {};

// We generate the inheritance to code

// and instantiated template List<int>

class MyClientList : public List<int> {};  

 

And here is one of the issues we run into.

 

If the code was this…

 

template <class T> List {};

template <> List <int> {};

 

class MyClientList : public List<int> {};

 

Our default as “int” is wrong, because the user intended to inherit from the primary template List<T> and not from List<int>. You can think of various other cases where keeping the default as "int" would generate wrong and even code that wouldn't compile.

 

I would like to get your feedback on what would be a good default template argument.

 

I was thinking that we will not try to generate any default. We will just plop the parameter names as they appear.

 

So for the above example when the user dragged an inheritance from MyClientList to List<T> we would generate…

 

template <class T> List {};

template <> List <int> {};

 

// Note that the default template

// argument in now “T” instead of “int”

class MyClientList : public List<T> {};

 

 

Yes it doesn’t compile, but is it better than default always being “int”? And also note that you can select the inheritance line and change the template arguments.

 

Please post your feedback and I am sure you will have some better suggestions.

 

 

Published Thursday, October 14, 2004 9:22 PM by rakeshna

Comments

 

Anthony Williams said:

Picking a default is a bad plan, because as you say above, there is not necessarily a good default to use. Better is to leave the template parameters unspecified in a way that guarantees they won't compile (and aren't subject to accidentally defaulting to real in-scope types with the same name as the parameters), or to prompt the user at the time
October 15, 2004 5:13 AM
 

Drazen Dotlic said:

Just don't pick any default, since there isn't a correct one.
One more thing - List<int> is valid type, and the code will most likely compile, so the developer might forget to replace int with a "real" type - one more reason not to put "int" there.
October 15, 2004 7:28 AM
 

Maribudu Janardhana said:

I completely agree with the above 2 comments. This is the best course of action ...
October 15, 2004 3:08 PM
 

Amit Joshi said:

I don't know if this is what you intend to do, but one trick is like this:

template < class T = int >
class MyClientList : public List<T>
{
};
October 15, 2004 3:27 PM
 

Ramnath Mukabar Singh said:

So what did u guys agree on doing?
October 19, 2004 5:22 PM
 

Rakesh Namineni said:

We decided not to use the parameter names as such.

We will generate this...

template <class T> List {};

template <> List <int> {};

class MyClientList : public List<T> {}; // Use T
October 29, 2004 2:42 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker