c++ - Why void* as template parameter works as function parameter but not template parameter? -
i have 2 version of my_begin:
template<typename t, typename std::enable_if<std::is_array<t>::value>::type* = 0> typename std::decay<t>::type my_begin(t& array) { homecoming array; } and
template<typename t> typename std::decay<t>::type my_begin(t& array, typename std::enable_if<std::is_array<t>::value>::type* = 0) { homecoming array; } however first 1 not work , gives error:
int a[10]; int* a_it = my_begin(a); error:
main.cpp:17:30: note: template argument deduction/substitution failed: main.cpp:16:80: error: not convert template argument '0' 'std::enable_if<true, void>::type* {aka void*}' template<typename t, typename std::enable_if<std::is_array<t>::value>::type* = 0> but sec 1 works. when alter 0 in first 1 nullptr, works (but still not working null). understand in template requires explicit casting (in case, int void*, why sec 1 not require it?
another question, if remove whitespace between * , =, failed. why that?
§14.1 [temp.param]/p4 says:
a non-type template-parameter shall have 1 of next (optionally cv-qualified) types:
integral or enumeration type, pointer object or pointer function, lvalue reference object or lvalue reference function, pointer member,std::nullptr_t. read literally, disallows void* template parameters altogether. void* object pointer type isn't pointer object type (§3.9.2 [basic.compound]/p3):
the type of pointer void or pointer object type called object pointer type. [ note: pointer void not have pointer-to-object type, however, because void not object type. —end note ]
if assume it's defect , standard meant "object pointer type", using 0 , company still disallowed §14.3.2 [temp.arg.nontype]/p5 (emphasis added):
the next conversions performed on each look used non-type template-argument. if non-type template-argument cannot converted type of corresponding template-parameter programme ill-formed.
[...] for non-type template-parameter of type pointer object, qualification conversions (4.4) , array-to-pointer conversion (4.2) applied; if template-argument of typestd::nullptr_t, null pointer conversion (4.10) applied. [ note: in particular, neither null pointer conversion zero-valued integer literal (4.10) nor derived-to-base conversion (4.10) applied. although 0 valid template-argument non-type template-parameter of integral type, not valid template-argument non-type template-parameter of pointer type. however, both (int*)0 , nullptr valid template-arguments non-type template-parameter of type “pointer int.” —end note ] = 0 works function default arguments because subject normal conversion rules, allows integer literal value 0 convert null pointer, rather special rules template arguments.
if remove whitespace between * , =, failed. why that?
maximum munch. if whitespace removed, *= single token (the compound assignment operator). in c++03 when had set space between >s in std::vector<std::vector<int> >.
c++ templates c++11
No comments:
Post a Comment