home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / cplus / 1757 < prev    next >
Encoding:
Text File  |  1992-12-12  |  1.8 KB  |  56 lines

  1. Path: sparky!uunet!newsserver.pixel.kodak.com!laidbak!tellab5!mcdchg!att!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: char * to const char*&
  5. Message-ID: <24377@alice.att.com>
  6. Date: 10 Dec 92 13:26:41 GMT
  7. Article-I.D.: alice.24377
  8. References: <JPOLITO.92Dec9155725@sysgem1.encore.com>
  9. Reply-To: ark@alice.UUCP ()
  10. Distribution: comp
  11. Organization: AT&T Bell Laboratories, Murray Hill NJ
  12. Lines: 42
  13.  
  14. In article <JPOLITO.92Dec9155725@sysgem1.encore.com> jpolito@sysgem1.encore.com (Jonathan Polito) writes:
  15.  
  16. > I am curious to what the current wisdom is on accepting char* as a
  17. > valid argument to a function expecting const char*&
  18.  
  19. It's invalid, though the reason may not be obvious at first glance.
  20.  
  21. The main thing to realize is that const char*& means "reference to
  22. pointer to const char" and not "reference to constant pointer to
  23. char" as one might think at first.
  24.  
  25. That means that in order to accept a char* as an argument, it would
  26. be necessary to convert the char* to a temporary of type const char*
  27. and then bind a reference to that temporary.  However, the reference
  28. does not refer to a const object (because a pointer to const is not
  29. itself a const), so it must be bound to a variable of the same type and
  30. not a temporary.
  31.  
  32. The thing that makes this example tricky is that if we define
  33.  
  34.     typedef char* T;
  35.  
  36. then const T and const char* represent different types! -- const T is
  37. "constant pointer to char" and const char* is "pointer to constant char".
  38.  
  39. The way to write "constant pointer to char" without a typedef it
  40. char *const&, so the following should be OK:
  41.  
  42.     extern void f(char *const&);
  43.  
  44.     main()
  45.     {
  46.         char* p = /* some value */;
  47.         f(p);
  48.         // ...
  49.     }
  50.  
  51. Here, p is of type "pointer to char" and there is no problem binding to
  52. p a "reference to constant pointer to char".
  53. -- 
  54.                 --Andrew Koenig
  55.                   ark@europa.att.com
  56.