Re: Const correctness and fun
Re: Const correctness and fun
- Subject: Re: Const correctness and fun
- From: David Fang <email@hidden>
- Date: Thu, 16 Jun 2005 15:13:47 -0400 (EDT)
> well, since we are here ;-)
> a riddle, what is this:
>
> typedef char const * (ns::X:: * const MF) (const char * const *) const;
Not sure if you're asking this just for sheer amusement,
but the answer is:
"MF is a const member-of ns::X function const pointer
that takes a const pointer to const chars,
and returns a pointer to const chars"
(something like a string-transforming member function)
While we're on the subject of fun, having nearly beaten this subject
half to death... :) Here's the general idea of code that unravels types
to human-readable strings. (Pardon typos, I'm writing this inline in
the email in haste.)
---------------- begin code -------------
#include <typeinfo> // for typeid()
#include <string>
using std::string;
// generic template definition, the canonical type name
template <class T>
struct what {
/// the type of the name (something printable)
typedef const char* name_type;
/// the full name of the type
static name_type name(void);
}; // end struct what
// partial specialization
template <class T>
struct what<const T> {
typedef const char* name_type;
static name_type name(void);
};
// partial specialization
template <class T>
struct what<T*> {
typedef const char* name_type;
static name_type name(void);
};
template <class T>
typename what<T>::name_type
what<T>::name(void) {
static const name_type local_name = typeid(T).name();
return local_name;
}
template <class T>
typename what<const T>::name_type
what<const T>::name(void) {
static const string name_string =
string(what<T>::name()) + " const";
static const char* const local_name = name_string.c_str();
return local_name;
}
template <class T>
typename what<T*>::name_type
what<T*>::name(void) {
static const string name_string = string(what<T>::name()) + "*";
static const char* const local_name = name_string.c_str();
return local_name;
}
------------- demo code ------------------
#include "thecodeabove.h"
#include <iostream>
using std::cout;
using std::endl;
class foo {};
void
main(void) {
cout << what<foo>::name() << endl;
cout << what<foo*>::name() << endl;
cout << what<foo const>::name() << endl;
cout << what<foo const*>::name() << endl;
cout << what<foo* const>::name() << endl;
cout << what<foo const* const>::name() << endl;
cout << what<foo******>::name() << endl;
cout << what<foo**const**const**>::name() << endl;
// ad nauseum, ad infinitum
}
------------- end code -------------------
And often you want to provide full specializations for certain classes,
especially those nested deeply in namespaces and classes, else you
get horrible C++-mangled names.
This may be generalized to accomodate references, template classes,
functions, member functions, and their arguments and their return types.
However, in the interest of me getting back to work, that is left as an
exercise to the easily-amused reader. I'm almost certain code like this
has been reinvented as much as the wheel if you Google it.
Cheers,
typedef
const Unreadable* (CornellUniv::*DavidFang) (Readable const* const) const;
const DavidFang needs_to_get_back_to_work;
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden