From -6448654160058943641 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,ede379f5dbbc1549 X-Google-Attributes: gidf78e5,public From: Kevin Kostrzewa Subject: Re: Suggestion: "typeof" keyword Date: 1999/08/24 Message-ID: #1/1 X-Deja-AN: 516771656 X-NNTP-Posting-Host: n31.creativesol.com Approved: stephen.clamage@sun.com (comp.std.c++) References: <7nt3vl$1oe$1@nnrp1.deja.com> <7oh7o8$t3f$1@nnrp1.deja.com> <7ol6hi$ej9$2@news.BelWue.DE> <7oup0a$8g2$1@nnrp1.deja.com> <37B3C2AD.5AB722AE@lucent.com> <37B5A571.449B@wanadoo.fr> <37B908A8.717CAC2C@lucent.com> <7pcvjs$tk4$1@nnrp1.deja.com> <37BBE6CC.FE2475E5@physik.tu-muenchen.de> X-UID: 0000000001 X-Status: $$$T Organization: Msen, Inc. Newsgroups: comp.std.c++ Originator: clamage@taumet sbnaran@uiuc.edu (Siemel B. Naran) writes: > I think that allowing "::" to be applied to the return types of > functions but not introducing the 'typeof' keyword would be a > better idea than introducing the 'typeof' keyword but leaving > the "::" rules alone. Then we could implement struct typeof in > terms of the existing core language. This saves us the ordeal > of figuring out the semantics of 'typeof'. I totally agree, but I'd like :: to be able to be applied to any expression, not just a function return type. If, as Siemel suggested, :: could be applied to the return type of a function, typeof could be written in terms of the language, but it would have to have a slightly different syntax if you wanted the typeof an expression vs. the typeof a type: template struct Typeof_tp { typedef T type; }; template Typeof_tp typeof(T) { return Typeof_tp(); } template Typeof_tp typeof() { return Typeof_tp(); } class Foo; typeof()::type t; Foo f; typeof(f)::type t; Also, if typeof is written in terms of the language, can we be guaranteed that the expression passed down to the typeof() function that accepts one parameter is not evaluated (like we are guaranteed for sizeof() when it is passed an expression?) That might hinder typeof(*(int *)0 + *(double *)0)::type t; Finally, I think that the people who want declare, as well as typeof can be appeased as well, as something like declare could be written using the language: // warning - this code will not compile! plus, it's pretty damn // ugly. template class Declare_tp { public: struct initialized_type { initialized_type() : m_t(*Declare_tp::s_ptcur) {} operator T &() { return m_t; } operator const T &() const { return m_t; } T *operator&() { return &m_t; } const T *operator&() const { return &m_t; } T &operator=(const T &other) { m_t = other; return *this; } private: T m_t; }; static const T *s_ptcur; Declare_tp(const T &tcur) { s_ptcur = &tcur; } typedef T type; }; template const T *Declare_tp::s_ptcur; // usage: int x = 1; double y = 2.5; declare(x+2y)::initialized_type t; declare(x+2y)::type t2; // t should be 6.0 and convertable to double // t2 should be of type double, uninitialized -- kevin kostrzewa work: kkostrzewa@csisolutions.com home: tkkost@newsguy.com [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]