c++ - Template argument type deduction by conversion operator -
i see example c++ 11 standard (n3337, 14.8.2.3/7)
struct { template <class t> operator t***(); }; a; const int * const * const * p1 = a; // t deduced int, not const int and try reproduce different compilers. changed example little adding declaration type t in conversion function
struct { template <class t> operator t***() { t t; //if t==const int, error (uninitialized const) return nullptr; } }; a; const int * const * const * p1 = a; int main(){} all compilers (vs2014, gcc 5.1.0 , clang 3.5.1) give error in declaration of "t", means t deduced const int. why that? extension?
this covered cwg issue #349, opened developer of edg c++ front end (which apparently deduces int, not const int):
we ran issue concerning qualification conversions when doing template argument deduction conversion functions.
the question is: type of t in conversion functions called example? t "int" or "const int"?
if t "int", conversion function in class works , 1 in class b fails (because return expression cannot converted return type of function). if t "const int", fails , b works.
because qualification conversion performed on result of conversion function, see no benefit in deducing t const int.
in addition, think code in class more occur code in class b. if author of class planning on returning pointer const entity, expect function have been written const in return type.
consequently, believe correct result should t int.
struct { template <class t> operator t***() { int*** p = 0; return p; } }; struct b { template <class t> operator t***() { const int*** p = 0; return p; } }; int main() { a; const int * const * const * p1 = a; b b; const int * const * const * p2 = b; }we have implemented feature, , pending clarification committee, deduce t int. it appears g++ , sun compiler deduce t const int.
this brought quoted paragraph existence (it didn't exist in c++03!), , presumably overlooked compiler developers.
Comments
Post a Comment