c++ - cast operator function compiles fine in g++ but not in other compilers. Why? -
this question has answer here:
consider following program:
struct s { using t = float; operator t() { return 9.9f; } }; int main() { s m; s::t t = m; t = m.operator t(); // correct ? }
the program compiles fine in g++ ( see live demo here )
but fails in compilation in clang++, msvc++ & intel c++ compiler
clang++ gives following errors ( see live demo here )
main.cpp:8:20: error: unknown type name 't'; did mean 's::t'? t = m.operator t(); // correct ? ^ s::t main.cpp:2:11: note: 's::t' declared here using t = float;
msvc++ gives following errors ( see live demo here )
source_file.cpp(8): error c2833: 'operator t' not recognized operator or type source_file.cpp(8): error c2059: syntax error: 'newline'
intel c++ compiler rejects code ( see live demo here )
so, question compiler right here ? g++ incorrect here or other 3 compilers incorrect here ? c++ standard says ?
if id-expression conversion-function-id, its conversion-type-id first looked in class of object expression , name, if found, used. otherwise looked in context of entire postfix-expression. in each of these lookups, names denote types or templates specializations types considered. [ example:
struct { }; namespace n { struct { void g() { } template <class t> operator t(); }; } int main() { n::a a; a.operator a(); // calls n::a::operator n::a }
— end example]
this indicates example fine, although in above example, a
has been declared type name, visible main
.
this discussed in core issue 156, filed way in 1999:
how about:
struct { typedef int t; operator t(); }; struct b : { operator t(); } b; void foo() { b.a::operator t(); // 2) error t not found in context // of postfix-expression? }
is interpretation correct? or intent error if
t
found in both scopes , referred different entities?erwin unruh: intent in both contexts. if find once, that's symbol. if find in both, both symbols must "the same" in respect. (if don't find it, error).
so i'd clang wrong: intent, expressed in wording extent, find t
, if in class.
Comments
Post a Comment