Saturday, 15 March 2014

c++ - Why does VC++ allow an instance of a template class without full template parameters? -



c++ - Why does VC++ allow an instance of a template class without full template parameters? -

in vc++ 2013's c++ header file memory, find class unique_ptr defined follows:

template<class _ty, class _dx> // = default_delete<_ty> class unique_ptr { ... };

what makes me confused is: template parameter doesn't have default type, required c++11 standard. (see here)

however, can compile next code without warning or error:

#include <memory> using namespace std; int main() { unique_ptr<int>(new int); // should ok! ??? // rather unique_ptr<int, default_delete<int>>(new int); }

why?

the default argument specified in prior forwards declaration:

// [memory:24] _std_begin #if _has_cpp0x template<class _ty> struct default_delete; template<class _ty, class _dx = default_delete<_ty> > class unique_ptr; #endif /* _has_cpp0x */ // [...] // [memory:1276] // template class unique_ptr scalar template<class _ty, class _dx> // = default_delete<_ty> class unique_ptr : private _unique_ptr_base<_ty, _dx, is_empty<_dx>::value || is_same<default_delete<_ty>, _dx>::value> { // non-copyable pointer object

this valid valid declare default argument function before definition, e.g.

void foo(int x = 5); void foo(int x) { /* ... */ }

[c++11: 14.1/10]: set of default template-arguments available utilize template declaration or definition obtained merging default arguments definition (if in scope) , declarations in scope in same way default function arguments (8.3.6). [ example:

template<class t1, class t2 = int> class a; template<class t1 = int, class t2> class a;

is equivalent to

template<class t1 = int, class t2 = int> class a;

—end illustration ]

c++ templates visual-c++ c++11 unique-ptr

No comments:

Post a Comment