Reply to comment

When to use class vs. typename in a Template Declaration

When declaring a template you can choose either "class" or "typename" for a template parameter.

Example:

template<typename T>
class vector
{
  // ...
};

or

template<class T>
class vector
{
  // ...
};

So, which should you use, when? According to C++ in a Nutshell (pg 322) it doesn't matter. The two are interchangeable in template declarations.

I have read that you should consider using "typename" when you want to document that your template is meant to work with any type and "class" when you want to document that it is designed to work with more complex data structures.

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.