From: Dave Thompson <david.thompson1@worldnet.att.net>
Subject: Re: Typing in Ada
Date: Thu, 10 Jun 2004 03:01:00 GMT
Date: 2004-06-10T03:01:00+00:00 [thread overview]
Message-ID: <g52dc051gc1k3p7tgevohoauea62416b8c@4ax.com> (raw)
In-Reply-To: Xns94FB4BB2972EApchapinsovernet@207.106.92.237
On Tue, 01 Jun 2004 11:26:30 GMT, "Peter C. Chapin"
<pchapin@sover.net> wrote:
> tmoran@acm.org wrote in news:XMTuc.35148$n_6.31103@attbi_s53:
>
> > I assumed by "C" you really meant something newer, like "C++". In "C"
> > "the only operations that you can perform on a structure are take its
> > address with &, and access one of its members." (The C Programming
> > Language, Kernighan and Ritchie).
If you are reading the first edition aka "K&R1", that is out of date.
Since the early '80s and in particular in C89 and later, you can also
assign a (whole, fixed size) struct, and as a logical consequence pass
it as an argument (all arguments are by value in C) or return it as a
function value. But yes only in C++ can you overload on struct types,
or indeed any other.
> > In any case, I'd be curious to see how you would write the above in C++.
>
Sorry I'm only online intermittently so I couldn't download the
previous reference in reasonable time. I'm responding on the
assumption it manipulates and displays (representations of)
polynomials in something like the obviously (to me) implied way.
> C++ supports operator overloading as applied to enums. It doesn't have an
> exponentiation operator, however, and you can't create new operators so that
> would create a technical inconvenience in this case. Off hand I'm not sure
You could overload enum varid ^ int exponent, since there would be no
use here for the usual C and C++ meaning as exclusive-or, but it has
rather low precedence with the other bitwise operators (which can't be
changed) hence couldn't be used as conveniently.
> how one would create a function template in C++ that could take an
> enumeration type and print the names of the enumerators. I wouldn't want to
> say it's impossible though. In any case I'm not sure how that's related to
> the issue of strong typing.
>
You can't; enum names are solely compile time in both C and C++,
except for nonstandard and (very) platform-dependent debugging. What
you can do is a preprocessor trick, something like:
#define Vars_List Var1(X) Var1(Y) Var1(Z)
#define Var1(id) id , /* in enum definition */
enum Vars { Vars_List }; /* note trailing comma allowed in C99,
in unextended C89 have to hack this slightly or add a dummy name,
which can actually be useful anyway for idiomatic half-open [0,N) */
#undef Var1
#define Var1(id) # id , /* in table of names */
const char * Vars_names [] = { Vars_List };
#undef Var1
/* now Y=1 and Vars_names[Y] is the string "Y" etc. */
Alternatively, instead of actual enum values, you could in C++ define
and use instances of a class type that collapse into the needed data:
class Term {
int varid, expon; double factor;
public:
/*ctor*/ Term (int varid)
: varid (varid), expon (1), factor (1.0) { }
/* could use a Freevar method that doublechecks varid is in the
valid range, known at startup but not compiletime; but if this
is made private and the Freevar method a friend,
invalid calls are prevented at compile time barring cheating */
Term& operator^= (int expon)
{ this->expon *= expon; return *this; }
Term operator^ (int expon)
{ Term that (*this); return that ^= expon; }
Term& operator*= (double factor)
{ this->factor *= factor; return *this; }
Term operator* (double factor)
{ Term that (*this); return that *= factor; }
};
class Freevar {
/*private:*/
typedef std::vector <const char *> var_names_t;
static var_names_t var_names;
int varid;
public:
/*ctor*/ Freevar (const char *this_name)
/* save name-string and "return" subscript thereto */
{
var_names_t::iterator new_item =
var_names.insert (var_names.end(), this_name);
this->varid = new_item - var_names.begin();
}
static const char * get_name (int id)
{ return var_names [id]; /* or safer .at(id) */ }
/*conversion*/ operator Term () const
{ return Term (this->varid); }
/*converting*/ Term operator^ (int expon) const
{ return Term (*this) ^ expon; }
};
Freevar::var_names_t Freevar::var_names; /* "initialize" */
const Freevar X ("X"); /* gets id 0 */
const Freevar Y ("Y"); /* gets id 1 */
const Freevar Z ("Z"); /* gets id 2 */
/* these particular id values guaranteed only if all (static)
definitions in same source file/module; if in separate modules
they will be unpredictable but unique, which is enough */
Now Y ^ 3 yields a Term containing an exponent of 3 for id 1, and say
Term::output can use Freevar::get_name (1) to get "Y".
If you want to do this for multiple "enums" I think you can templatize
it but I haven't worked that one out in detail.
Or as a real hack you could make the values (singleton instances of)
classes that inherit with at least one virtual method, thereby
providing RTTI (Run-Time Type Information) whose implementation
dependent values you then decode somehow. Yuck.
It probably should (but won't) go without saying that this is more
work and less simple than built-in compiler support as in Ada.
Additional minor point: in C you can't really return a string as a
function value. You must either store into a caller-provided buffer;
return a pointer to static space (not threadsafe and dubious if used
multiple times); or return a pointer to allocated space (caller must
free). In C++ you can return a std::string; (in practice though not
formally required) this is really a pointer to heap space, but is
managed automatically.
- David.Thompson1 at worldnet.att.net
next prev parent reply other threads:[~2004-06-10 3:01 UTC|newest]
Thread overview: 67+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-05-31 13:32 Typing in Ada Empit
2004-05-31 14:04 ` Poul-Erik Andreasen
2004-05-31 17:01 ` Jeffrey Carter
2004-05-31 20:03 ` Peter C. Chapin
2004-05-31 22:56 ` tmoran
2004-06-01 1:09 ` Peter C. Chapin
2004-06-01 4:40 ` tmoran
2004-06-01 11:26 ` Peter C. Chapin
2004-06-10 3:01 ` Dave Thompson [this message]
2004-06-10 3:00 ` Dave Thompson
2004-05-31 23:22 ` Nick Roberts
2004-06-01 1:04 ` Peter C. Chapin
2004-06-01 2:29 ` Nick Roberts
2004-06-02 4:39 ` Robert I. Eachus
2004-06-02 15:17 ` Hyman Rosen
2004-06-01 2:36 ` Hyman Rosen
2004-06-01 4:27 ` Larry Kilgallen
2004-06-01 4:05 ` Hyman Rosen
[not found] ` <d4vnb0tepd4togdrvdrbqpok1ne6n9i2vp@4ax.com>
2004-06-01 14:36 ` Wes Groleau
2004-06-01 20:24 ` Niklas Holsti
2004-06-02 4:43 ` Wes Groleau
2004-06-02 5:28 ` Robert I. Eachus
2004-06-02 8:19 ` tmoran
2004-06-02 14:47 ` Wes Groleau
2004-06-02 11:26 ` Marin David Condic
2004-06-02 14:54 ` gratuitous restrictions (was:Typing in Ada) Wes Groleau
2004-06-02 5:04 ` Typing in Ada Robert I. Eachus
2004-06-01 2:14 ` David C. Hoos
2004-06-02 1:30 ` Jeffrey Carter
2004-06-02 10:53 ` Peter C. Chapin
2004-06-02 11:38 ` Marin David Condic
2004-06-17 2:50 ` Dave Thompson
2004-06-17 4:24 ` James Rogers
2004-06-17 12:28 ` Hyman Rosen
2004-06-17 23:42 ` James Rogers
2004-06-20 11:27 ` Nick Roberts
2004-06-20 23:29 ` new revision ada Brian May
2004-06-21 2:16 ` tmoran
2004-06-21 2:34 ` James Rogers
2004-06-22 2:16 ` Roland Illig
2004-06-22 3:41 ` James Rogers
2004-06-22 6:53 ` Martin Krischik
2004-06-21 23:33 ` Brian May
2004-06-22 20:26 ` Simon Wright
2004-06-23 0:50 ` Larry Elmore
2004-06-22 22:06 ` tmoran
2004-06-21 5:31 ` Wes Groleau
2004-06-21 12:27 ` new revision ada (limited with, excpetion handling) Nick Roberts
2004-06-21 13:04 ` Martin Dowie
2004-06-22 10:38 ` new revision ada Georg Bauhaus
2004-06-22 12:45 ` James Rogers
2004-06-22 15:17 ` Martin Krischik
2004-06-22 16:09 ` new revision ada (exception handling) Nick Roberts
2004-06-23 7:55 ` Pascal Obry
2004-06-23 8:40 ` Martin Krischik
2004-06-23 19:33 ` Randy Brukardt
2004-06-24 6:57 ` Martin Krischik
2004-06-24 21:13 ` Randy Brukardt
2004-06-25 8:05 ` Dmitry A. Kazakov
2004-06-25 17:28 ` Randy Brukardt
2004-06-23 4:31 ` new revision ada Brian May
2004-06-23 19:47 ` Randy Brukardt
2004-06-22 16:37 ` Georg Bauhaus
2004-06-26 14:57 ` Robert I. Eachus
2004-06-01 1:02 ` Typing in Ada Alexander E. Kopilovich
-- strict thread matches above, loose matches on Subject: below --
2004-06-01 2:11 David C. Hoos, Sr.
2004-06-01 2:13 David C. Hoos, Sr.
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox