1

I'm new to C++ and I am have difficulty understanding this code:

template <typename T = unsigned> 
  1. What is T = unsigned means?
  2. Does the compiler enforce the unsigned on the given type?
2
  • 3. List item – what? BTW, unsigned is a synonym for unsigned int in this context (and many others). Commented Jul 23, 2015 at 19:53
  • It's a default template parameter type. Commented Jul 23, 2015 at 19:54

2 Answers 2

6

That's a default template parameter; it is similar to a default function parameter. If you don't put in an argument, it will default to unsigned [int]. So imagine this:

template <typename T = unsigned> struct foo { T one; T two; }; 

If I declare for example a foo<char>, the resulting structure will have two char members. But the default parameter lets me declare a foo<>, and that structure will have two unsigned int members, because unsigned int is the default.

Sign up to request clarification or add additional context in comments.

Comments

1

The template has a default parameter for the type T, in this case unsigned int.

The unsigned is short hand for unsigned int.

For example; in client code if the template was a class template, then an object could be declared with or without explicitly adding a type to the declaration;

ABC<> abc1; // the <> is required ABC<unsigned int> abc2; // equivalent type to abc1 ABC<float> abc3; 

Related question, regarding the syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.