以下摘自The Go Programming Language

Each call to new returns a distinct variable with a unique address:
p := new(int)
q := new(int)
fmt.Println(p == q) // “false”
There is one exception to this rule: two variables whose type carries no information and is therefore of size zero, such as struct{} or [0]int, may, depending on the implementation, have the same address.
The new function is relatively rarely used because the most common unnamed variables are of struct types, for which the struct literal syntax is more flexible.

Since new is a predeclared function, not a keyword, it’s possible to redefine the name for something else within a function, for example:
func delta(old, new int) int { return new – old }
Of course, within delta, the built-in new function is unavailable.




template <class T> ...  


template <typename T> ...  

大多数情况下,二者可以交替使用。但有些情况,比如template template parameter只允许classC++1Z已经提交了关于template template parameter允许typename建议,有些编译器已经支持了)。

Use ‘class’ or ‘typename’ for template parameters?
Why does not a template template parameter allow ‘typename’ after the parameter list

typename另一个用法是在模板的定义中(选自The typename keyword (C++ only)):

Use the keyword typename if you have a qualified name that refers to a type and depends on a template parameter. Only use the keyword typename in template declarations and definitions. Consider the following example:

template<class T> class A
  typedef char C;
  A::C d;

The statement T::x(y) is ambiguous. It could be a call to function x() with a nonlocal argument y, or it could be a declaration of variable y with type T::x. C++ compiler interprets this statement as a function call. In order for the compiler to interpret this statement as a declaration, you must add the keyword typename to the beginning of T:x(y). The statement A::C d; is ill-formed. The class A also refers to A and thus depends on a template parameter. You must add the keyword typename to the beginning of this declaration:

typename A::C d;

You can also use the keyword typename in place of the keyword class in the template parameter declarations.



A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.


为了深入理解typename,需要了解dependent name。这篇文章解释地很好:

To make this work, the C++ standard defines a “two-phase name lookup” rule for names in templates. Names inside templates are divided to two types:

Dependent – names that depend on the template parameters but aren’t declared within the template.
Non-dependent – names that don’t depend on the template parameters, plus the name of the template itself and names declared within it.

When the compiler tries to resolve some name in the code, it first decides whether the name is dependent or not, and the resolution process stems from this distinction. While non-dependent names are resolved “normally” – when the template is defined, the resolution for dependent names happens at the point of the template’s instantiation. This is what ensures that a specialization can be noticed correctly in the example above.