Topic: typeinfo.h and const char const *


Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: Mon, 15 Apr 2002 12:14:31 GMT
Raw View
"Stephen Clamage" <stephen.clamage@sun.com> wrote in message
>
> But "const char *" and "char const *" are exactly the same type, so
> you should expect to see the same representation of the name. The
> declaration "const char const *" isn't valid, due to repeated "const"
> qualifiers, except when generated from a template declaration. In that
> case it is the same as the other two declarations!

What he should try is "const char *" vs "char * const".



Philippe Bouchard
http://fornux.com:8080/


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Mon, 15 Apr 2002 15:35:48 GMT
Raw View
On Mon, 15 Apr 2002 12:14:31 GMT, "Philippe A. Bouchard"
<philippeb@videotron.ca> wrote:

>What he should try is "const char *" vs "char * const".

Thanks for pointing this out, which I did try yesterday. My compiler
returns

    "char *"

for "char * const" and

    "const char *"

for "const char * const". It seems that the "const"-ness of the
pointer is irrelevant to the type, but not so the "const"-ness of the
object it points to.

Obviously, comparing typeid's is far better.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: Tue, 16 Apr 2002 01:48:24 GMT
Raw View
"Stephen Clamage" <stephen.clamage@sun.com> wrote in message
> >The problem is that <typeinfo.h> (or my compiler, BCB5) doesn't seem
> >to differentiate between "const char *" and "char const *" and "const
> >char const *". In each case, I get "const char *" as type name; only
> >if const is missing entirely does it return "char *".
>
> But "const char *" and "char const *" are exactly the same type, so
> you should expect to see the same representation of the name. The
> declaration "const char const *" isn't valid, due to repeated "const"
> qualifiers, except when generated from a template declaration. In that
> case it is the same as the other two declarations!

What he should try is "const char *" vs "char * const".



Philippe Bouchard
http://fornux.com:8080/


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Ron Natalie <ron@sensor.com>
Date: Tue, 16 Apr 2002 16:25:10 GMT
Raw View

"Philippe A. Bouchard" wrote:

> What he should try is "const char *" vs "char * const".

he could try that, but there isn't going to be a difference
between:
 char * const
and char *

The top level cv qualification is ignored.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Sat, 13 Apr 2002 15:21:47 GMT
Raw View
I'm writing some wrapper classes for ODBC database access and have to
verify that the SQL data type (passed as an integer code such as
SQL_CHAR, SQL_INTEGER etc.) is consistent with the actual data used.
(This is all not very OOP, but that's why I'm writing wrapper classes
in the first place :)

Anyway, here's a template function I have:

#include <string>
#include <typeinfo.h>
// other #includes, etc. ...

namespace sqlutil {
  enum Sql_Type {
      /* etc. */
  };

  template < class T >
  bool verify ( const T & class_type, const Sql_Type & sql_type )
  {
      const type_info & t = typeid( class_type );
      std::string n( t.name() );

      switch ( sql_type )
      {
         case SQL_VARCHAR:
         case SQL_NUMERIC:
         case SQL_TIMESTAMP:
         // etc.
             if (
                  || (n == "char *")
                  || (n == "const char *")
                  || (n == "std::basic_string< <char> ... etc...")
                 )
                  return true;
         case SQL_DOUBLE:
               /* etc. ... you get the idea. */
}

The problem is that <typeinfo.h> (or my compiler, BCB5) doesn't seem
to differentiate between "const char *" and "char const *" and "const
char const *". In each case, I get "const char *" as type name; only
if const is missing entirely does it return "char *".

Questions:
(1) Is this normal behavior for typeinfo, or is it a shortcoming of my
compiler?
(2) Is typeinfo part of the ANSI-C++ standard?

The problem would be academic except that I need to make sure that I
have all the bases covered.

TIA,

Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Claus Rasmussen <clr@cc-consult.dk>
Date: Sat, 13 Apr 2002 17:26:38 GMT
Raw View
Bob Hairgrove wrote:

> (2) Is typeinfo part of the ANSI-C++ standard?

Yes. But the value returned by the typeinfo::name function is not defined
by the standard. gcc for example returns "Pc" for the 'char*' type. gcc
do differentiate between const and non-const types, though.

But I would suggest you to take another approach and look into the Boost
library (www.boost.org). I think you could make good use of their type-
traits library.

        -Claus

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Stephen Clamage <stephen.clamage@sun.com>
Date: Sat, 13 Apr 2002 19:20:49 GMT
Raw View
On Sat, 13 Apr 2002 15:21:47 GMT, rhairgroveNoSpam@Pleasebigfoot.com
(Bob Hairgrove) wrote:


>
>The problem is that <typeinfo.h> (or my compiler, BCB5) doesn't seem
>to differentiate between "const char *" and "char const *" and "const
>char const *". In each case, I get "const char *" as type name; only
>if const is missing entirely does it return "char *".

But "const char *" and "char const *" are exactly the same type, so
you should expect to see the same representation of the name. The
declaration "const char const *" isn't valid, due to repeated "const"
qualifiers, except when generated from a template declaration. In that
case it is the same as the other two declarations!

---
Steve Clamage, stephen.clamage@sun.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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 14 Apr 2002 06:58:34 GMT
Raw View
Bob Hairgrove wrote:
....
> Anyway, here's a template function I have:
>
> #include <string>
> #include <typeinfo.h>
> // other #includes, etc. ...
>
> namespace sqlutil {
>   enum Sql_Type {
>       /* etc. */
>   };
>
>   template < class T >
>   bool verify ( const T & class_type, const Sql_Type & sql_type )
>   {
>       const type_info & t = typeid( class_type );
>       std::string n( t.name() );
>
>       switch ( sql_type )
>       {
>          case SQL_VARCHAR:
>          case SQL_NUMERIC:
>          case SQL_TIMESTAMP:
>          // etc.
>              if (
>                   || (n == "char *")
>                   || (n == "const char *")
>                   || (n == "std::basic_string< <char> ... etc...")
>                  )
>                   return true;
>          case SQL_DOUBLE:
>                /* etc. ... you get the idea. */
> }
>
> The problem is that <typeinfo.h> (or my compiler, BCB5) doesn't seem
> to differentiate between "const char *" and "char const *" and "const
> char const *". In each case, I get "const char *" as type name; only
> if const is missing entirely does it return "char *".

Stephen Clamage has already addressed that part of your problem.

> Questions:
> (1) Is this normal behavior for typeinfo, or is it a shortcoming of my
> compiler?
> (2) Is typeinfo part of the ANSI-C++ standard?

Yes, typeinfo is part of the standard. However, the exact contents of
the string returned by name() are implementation-defined. It's perfectly
legal, for instance, for name() to return "" for all types. In general,
the only proper use for name() is to produce user-readable messages that
refer to a type. Even then, they won't necessarily be very useful
messages.

What you should be doing is using std::typeinfo::operator=(). In other
words, instead of testing whether n=="char*", you need to test whether
t==typeid(char*).

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 14 Apr 2002 15:33:15 GMT
Raw View
"James Kuyper Jr." wrote:
....
> What you should be doing is using std::typeinfo::operator=(). In other

Obviously, what I meant to say was operator==().

> words, instead of testing whether n=="char*", you need to test whether
> t==typeid(char*).

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]