 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ben Guest
|
Posted: Wed Oct 22, 2003 6:41 pm Post subject: Why not obj.Type? |
|
|
In C++, there's an interesting branch called meta-programming.
Expression Template is a famous tech in meta-programming.
It would be easy to write Matrix m=m1+m2-m3;
The computation will happen at the point of "=". Perfect.
But, what if I sometimes want to save some formula for later use?
Say,
Formula f = m1+m2-m3;//no computation is done yet.
if(...){
m = f + m4;//apply the formula and do the computation.
}
else{
m = f - m4;//apply the formula and do the computation.
}
Is that a reasonable request? But it would be extremely tedious to
hand write the type of the Formula. It would be something like:
MatrixBin<MatrixBin
This can go on and on and on.
Another application is if I want to develop an io formator library to
support printf-like formatting.
It would be ideal to say:
Fmt f = fmt<<"hello, your first name is:"<< _ <<", last name is:"<< _;
then later in subsequent code:
cout<
However, the type of fmt is hard to write, it could be something like:
Fmt2, FmtParam>,
FmtTerm<const char*> >,FmtParam>
An obvious solution would be to introduce type inference to C++. If
that is too hard, then at least C++ should support referencing nested
types from an object.
That is,
I can define a typedef named "Type" in the Fmt classes.
And then, a macro can suffice.
#define INFER(name, exp) (exp).Type name(exp)
INFER(f, fmt<<"hello"<< _ << "world");
It does not require any new keyword. All C++ has to do is to allow
refernecing type name from an object, like it does for referencing
static member from an object. Is that too demanding? Of course, the
parser has to be more sophisticated.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Thu Oct 23, 2003 2:35 pm Post subject: Re: Why not obj.Type? |
|
|
[email]ben_yu (AT) asc (DOT) aon.com[/email] (Ben) wrote in message news:<ff73f526.0310220908.25a7fadf (AT) posting (DOT) google.com>...
| Quote: | In C++, there's an interesting branch called meta-programming.
Expression Template is a famous tech in meta-programming.
It would be easy to write Matrix m=m1+m2-m3;
The computation will happen at the point of "=". Perfect.
But, what if I sometimes want to save some formula for later use?
|
You can use inline function for this. If you want it to work
without explicitly mentioning the Matrix types, use a template.
| Quote: | Say,
Formula f = m1+m2-m3;//no computation is done yet.
if(...){
m = f + m4;//apply the formula and do the computation.
}
else{
m = f - m4;//apply the formula and do the computation.
}
Is that a reasonable request? But it would be extremely tedious to
hand write the type of the Formula. It would be something like:
MatrixBin<MatrixBin
This can go on and on and on.
|
If you don't care much about efficiency, you can use operators +, -
etc. for your computations, with a couple of constructor/destructor/copy
constructors applied. If you would use data with ref counter, you'll
only have a significant amount of computation for creating/deleting
intermediate results.
| Quote: | Another application is if I want to develop an io formator library to
support printf-like formatting.
It would be ideal to say:
Fmt f = fmt<<"hello, your first name is:"<< _ <<", last name is:"<< _;
then later in subsequent code:
cout<
|
You can define 'f' as a function formatting the name etc. into a string.
| Quote: | However, the type of fmt is hard to write, it could be something like:
Fmt2, FmtParam>,
FmtTerm<const char*> >,FmtParam
An obvious solution would be to introduce type inference to C++. If
that is too hard, then at least C++ should support referencing nested
types from an object.
That is,
I can define a typedef named "Type" in the Fmt classes.
And then, a macro can suffice.
#define INFER(name, exp) (exp).Type name(exp)
INFER(f, fmt<<"hello"<< _ << "world");
It does not require any new keyword. All C++ has to do is to allow
|
It seems that the "Type" IS the new keyword you propose for C++.
| Quote: | refernecing type name from an object, like it does for referencing
static member from an object. Is that too demanding? Of course, the
parser has to be more sophisticated.
|
Well, in your cases you can use the Matrix or string type.
Many OO languages have this ".class" feature, Python is only one
example of that. Unfortunately, there are only a few uses
for metaprogramming on the level of your examples.
There is an easier way of doing this.
1.: More programming discipline, keeping account of the types of
your data. This includes using the same type where it seems
that you might need several types, e.g. the type of the result
of your format function could be "string" or "ostream&".
This might also help you debug your programs!-)
2. Using typedefs, templates, operators, and inline functions
for making the code more comprehensible and efficient.
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Guest
|
Posted: Thu Oct 23, 2003 10:44 pm Post subject: Re: Why not obj.Type? |
|
|
Let me rephrase my question first,
I want to be able to do this:
class X{
public:
typedef X Self;
};
X x;
x.Self x2;
No new keyword is necessary.
With this supported, I can do limited form of type inference as long
as the type itself is built with type inference in mind.
| Quote: | In C++, there's an interesting branch called meta-programming.
Expression Template is a famous tech in meta-programming.
It would be easy to write Matrix m=m1+m2-m3;
The computation will happen at the point of "=". Perfect.
But, what if I sometimes want to save some formula for later use?
You can use inline function for this. If you want it to work
without explicitly mentioning the Matrix types, use a template.
But how? ET requires the return type to be a very lengthy one. How can |
I avoid mentioning it? That's my whole point of this question.
| Quote: | Say,
Formula f = m1+m2-m3;//no computation is done yet.
if(...){
m = f + m4;//apply the formula and do the computation.
}
else{
m = f - m4;//apply the formula and do the computation.
}
Is that a reasonable request? But it would be extremely tedious to
hand write the type of the Formula. It would be something like:
MatrixBin<MatrixBin
This can go on and on and on.
If you don't care much about efficiency, you can use operators +, -
etc. for your computations, with a couple of constructor/destructor/copy
constructors applied. If you would use data with ref counter, you'll
only have a significant amount of computation for creating/deleting
intermediate results.
|
No. We are talking about Expression Template where we don't do any
computation until necessary. To achieve such laziness without resort
to virtual function, light-weight expression object with huge template
type name is necessary.
| Quote: |
Another application is if I want to develop an io formator library to
support printf-like formatting.
It would be ideal to say:
Fmt f = fmt<<"hello, your first name is:"<< _ <<", last name is:"<< _;
then later in subsequent code:
cout<
You can define 'f' as a function formatting the name etc. into a string.
However, the type of fmt is hard to write, it could be something like:
Fmt2, FmtParam>,
FmtTerm<const char*> >,FmtParam
An obvious solution would be to introduce type inference to C++. If
that is too hard, then at least C++ should support referencing nested
types from an object.
That is,
I can define a typedef named "Type" in the Fmt classes.
And then, a macro can suffice.
#define INFER(name, exp) (exp).Type name(exp)
INFER(f, fmt<<"hello"<< _ << "world");
It does not require any new keyword. All C++ has to do is to allow
It seems that the "Type" IS the new keyword you propose for C++.
|
See my rephrase.
| Quote: |
refernecing type name from an object, like it does for referencing
static member from an object. Is that too demanding? Of course, the
parser has to be more sophisticated.
Well, in your cases you can use the Matrix or string type.
Many OO languages have this ".class" feature, Python is only one
example of that. Unfortunately, there are only a few uses
for metaprogramming on the level of your examples.
There is an easier way of doing this.
1.: More programming discipline, keeping account of the types of
your data. This includes using the same type where it seems
that you might need several types, e.g. the type of the result
of your format function could be "string" or "ostream&".
This might also help you debug your programs!-)
2. Using typedefs, templates, operators, and inline functions
for making the code more comprehensible and efficient.
|
Thanks for the advices. But that won't help in this specific
meta-programming case.
And all I want is just to do exp.SomeType. I can do
exp.someStaticMember now, anyway.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
stelios xanthakis Guest
|
Posted: Fri Oct 24, 2003 7:25 pm Post subject: Re: Why not obj.Type? |
|
|
[email]ben_yu (AT) asc (DOT) aon.com[/email] (Ben) wrote in message news:<ff73f526.0310230850.2fbc1863 (AT) posting (DOT) google.com>...
| Quote: | Let me rephrase my question first,
I want to be able to do this:
class X{
public:
typedef X Self;
};
X x;
x.Self x2;
|
If you want to be able to do this, you'll soon want to be able
to do that:
{
X *x3 = new x.Self (x, 12);
}
Right now, the parser expects a type known at compile-time
after "new". Having a dynamic type after "new" is a much harder
thing. It means that: everyobject which can provide such dynamic
types should have some standard information about:
- boolean: I can provide the things below.
- which operator_new function to call
- what's the size of the object
- a list of all possible constructors!!!
- runtime overload resulution ??!?!
All these in the virtual table or something...
Is it worth it?
stelios
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Sat Oct 25, 2003 8:37 am Post subject: Re: Why not obj.Type? |
|
|
[email]ben_yu (AT) asc (DOT) aon.com[/email] (Ben) wrote in message news:<ff73f526.0310230850.2fbc1863 (AT) posting (DOT) google.com>...
| Quote: | Let me rephrase my question first,
I want to be able to do this:
class X{
public:
typedef X Self;
};
X x;
x.Self x2;
No new keyword is necessary.
With this supported, I can do limited form of type inference as long
as the type itself is built with type inference in mind.
In C++, there's an interesting branch called meta-programming.
Expression Template is a famous tech in meta-programming.
It would be easy to write Matrix m=m1+m2-m3;
The computation will happen at the point of "=". Perfect.
But, what if I sometimes want to save some formula for later use?
You can use inline function for this. If you want it to work
without explicitly mentioning the Matrix types, use a template.
But how? ET requires the return type to be a very lengthy one. How can
I avoid mentioning it? That's my whole point of this question.
Say,
Formula f = m1+m2-m3;//no computation is done yet.
if(...){
m = f + m4;//apply the formula and do the computation.
}
else{
m = f - m4;//apply the formula and do the computation.
}
Is that a reasonable request? But it would be extremely tedious to
hand write the type of the Formula. It would be something like:
MatrixBin<MatrixBin
This can go on and on and on.
If you don't care much about efficiency, you can use operators +, -
etc. for your computations, with a couple of constructor/destructor/copy
constructors applied. If you would use data with ref counter, you'll
only have a significant amount of computation for creating/deleting
intermediate results.
No. We are talking about Expression Template where we don't do any
computation until necessary. To achieve such laziness without resort
to virtual function, light-weight expression object with huge template
type name is necessary.
|
I'm sorry I didn't understand your mentioning of expression templates.
Now, after reading a paper about this, I'll try to propose a different
solution.
1. You can enhance your matrix class with "function" constructors,
where you give them a function ("expression") object and several
matrix/vector/constant arguments. Then you'll only need to write
"Matrix f(expr, arg1,..., argn);" for an expression with n arguments.
Defining an "expr" () operator and the constructors as inlined functions,
you'll have all the effectiveness of expression templates, without the need
of building an expression tree of templates. The only problem will be
the need to write a huge number of constructors (only once),
and the need to define your expressions outside the function body.
2. If you want a good and convenient language that is fast for numeric
computations, object oriented and functional (lambda exprs etc.) -
you can use Python with the Numeric package.
| Quote: | Another application is if I want to develop an io formator library to
support printf-like formatting.
It would be ideal to say:
Fmt f = fmt<<"hello, your first name is:"<< _ <<", last name is:"<< _;
then later in subsequent code:
cout<
You can define 'f' as a function formatting the name etc. into a string.
However, the type of fmt is hard to write, it could be something like:
Fmt2, FmtParam>,
FmtTerm<const char*> >,FmtParam
An obvious solution would be to introduce type inference to C++. If
that is too hard, then at least C++ should support referencing nested
types from an object.
That is,
I can define a typedef named "Type" in the Fmt classes.
And then, a macro can suffice.
#define INFER(name, exp) (exp).Type name(exp)
INFER(f, fmt<<"hello"<< _ << "world");
It does not require any new keyword. All C++ has to do is to allow
It seems that the "Type" IS the new keyword you propose for C++.
See my rephrase.
refernecing type name from an object, like it does for referencing
static member from an object. Is that too demanding? Of course, the
parser has to be more sophisticated.
|
Unfortunately, it's not the parser, it's the language. Adding
new keywords to the parser is easy. Making the language be
more meta-programming (C++ templates are a low level
meta-programming) is more complicated. You'll need
to convince people that it's worth to make C++
more complicated and therefore less comprehensible.
... skipping my irrelevant remarks
| Quote: | Thanks for the advices. But that won't help in this specific
meta-programming case.
And all I want is just to do exp.SomeType. I can do
exp.someStaticMember now, anyway.
|
My personal preferences are small and simple languages versus
the complicated ones. My favorites are awk, C, Python, and C++,
and not Perl, Modula and Ada!-)
Your proposal will make sloppy programming in C++ easier.
We'll be able to write programs where we have no idea what are
the types of our data.
The idea of a strongly-typed language is just the opposite -
the inventors of these languages wanted the programmer
(and the guy who will read his programs in the next 5 years)
to know what is the structure of their data. For meta-programming,
you might prefer to use less strongly-typed languages, like Lisp
or Python.
BTW, you can use the "typeof" operator of gcc. Is it defined in g++?
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Guest
|
Posted: Sat Oct 25, 2003 8:39 am Post subject: Re: Why not obj.Type? |
|
|
[email]mayall (AT) freemail (DOT) gr[/email] (stelios xanthakis) wrote in message news:<8a018872.0310240216.56726186 (AT) posting (DOT) google.com>...
| Quote: | ben_yu (AT) asc (DOT) aon.com (Ben) wrote in message news:<ff73f526.0310230850.2fbc1863 (AT) posting (DOT) google.com>...
Let me rephrase my question first,
I want to be able to do this:
class X{
public:
typedef X Self;
};
X x;
x.Self x2;
If you want to be able to do this, you'll soon want to be able
to do that:
{
X *x3 = new x.Self (x, 12);
}
Right now, the parser expects a type known at compile-time
after "new". Having a dynamic type after "new" is a much harder
thing. It means that: everyobject which can provide such dynamic
types should have some standard information about:
This is not related to dynamic type. It does not require any runtime |
support.
Every expression in C/C++ has a type statically. And x.Self is like to
say "find_the_static_type_of(x)::Self"
similar to the proposed typeof(x), except it does not require a new
keyword (not that powerful either)
The applications of this can be
1. Formula in Expression Template.
2. Scope Guard. (Don't have to use mutable variable and const
reference, can support manual rollback, commit)
3. a type safe io stream formattor.
Ben.
{excess quote snipped -mod/fwg}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Guest
|
Posted: Mon Oct 27, 2003 12:03 am Post subject: Re: Why not obj.Type? |
|
|
| Quote: | I'm sorry I didn't understand your mentioning of expression templates.
Now, after reading a paper about this, I'll try to propose a different
solution.
1. You can enhance your matrix class with "function" constructors,
where you give them a function ("expression") object and several
matrix/vector/constant arguments. Then you'll only need to write
"Matrix f(expr, arg1,..., argn);" for an expression with n arguments.
Defining an "expr" () operator and the constructors as inlined functions,
you'll have all the effectiveness of expression templates, without the need
of building an expression tree of templates. The only problem will be
the need to write a huge number of constructors (only once),
and the need to define your expressions outside the function body.
It does not seem to be able to save formulas. |
By formula, I mean things like: Formula f = m1+m2-m3;
Here, f is just a light-weight object with the expression syntax tree
built in it. No matrix computation is done, no memory allocation is
necessary.
| Quote: | 2. If you want a good and convenient language that is fast for numeric
computations, object oriented and functional (lambda exprs etc.) -
you can use Python with the Numeric package.
Another application is a type-safe io formattor library. Currently |
boost formatter library still uses printf-like string to describe the
format. And that is not type safe.
| Quote: | Unfortunately, it's not the parser, it's the language. Adding
new keywords to the parser is easy. Making the language be
more meta-programming (C++ templates are a low level
meta-programming) is more complicated. You'll need
to convince people that it's worth to make C++
more complicated and therefore less comprehensible.
I don't propose any new keyword. if I can do exp.someStaticMember, |
then why can't I do exp.someTypeDef?
| Quote: | My personal preferences are small and simple languages versus
the complicated ones. My favorites are awk, C, Python, and C++,
and not Perl, Modula and Ada!-)
Your proposal will make sloppy programming in C++ easier.
We'll be able to write programs where we have no idea what are
the types of our data.
The idea of a strongly-typed language is just the opposite -
the inventors of these languages wanted the programmer
(and the guy who will read his programs in the next 5 years)
to know what is the structure of their data. For meta-programming,
you might prefer to use less strongly-typed languages, like Lisp
or Python.
I am not convinced that type inference can undermine the notion of |
strong type. Many strong typed functional language (like haskell,ml)
support type inference.
And even if some people may not like general type inference scheme
like "typeof()", this does not seem that controversial as "typeof"
though because it does require an explicit typedef in the class
definition. You cannot simply say
typeof(1) t = 1;
the class has to be built with type-inference in mind. i.e. (The class
author decides if this class needs type inference)
And there are some applications that you don't want the programmer to
write the type explicitly, such as scope guard.
| Quote: | BTW, you can use the "typeof" operator of gcc. Is it defined in g++?
Michael
I'd like to see such operator in standard C++. It will for sure meet |
my needs. But as you pointed out, this requires an extra keyword
though. I can be satisfied by a less demanding feature: ability to
reference typedef from an object of the class.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
TB Guest
|
Posted: Tue Oct 28, 2003 2:14 am Post subject: Re: Why not obj.Type? |
|
|
[email]ben_yu (AT) asc (DOT) aon.com[/email] (Ben) wrote in message news:<ff73f526.0310241828.5a6ee5ce (AT) posting (DOT) google.com>...
| Quote: | mayall (AT) freemail (DOT) gr (stelios xanthakis) wrote in message news:<8a018872.0310240216.56726186 (AT) posting (DOT) google.com>...
[email]ben_yu (AT) asc (DOT) aon.com[/email] (Ben) wrote in message news:<ff73f526.0310230850.2fbc1863 (AT) posting (DOT) google.com>...
Let me rephrase my question first,
I want to be able to do this:
class X{
public:
typedef X Self;
};
X x;
x.Self x2;
If you want to be able to do this, you'll soon want to be able
to do that:
{
X *x3 = new x.Self (x, 12);
}
Right now, the parser expects a type known at compile-time
after "new". Having a dynamic type after "new" is a much harder
thing. It means that: everyobject which can provide such dynamic
types should have some standard information about:
This is not related to dynamic type. It does not require any runtime
support.
Every expression in C/C++ has a type statically. And x.Self is like to
say "find_the_static_type_of(x)::Self"
similar to the proposed typeof(x), except it does not require a new
keyword (not that powerful either)
The applications of this can be
1. Formula in Expression Template.
2. Scope Guard. (Don't have to use mutable variable and const
reference, can support manual rollback, commit)
3. a type safe io stream formattor.
Ben.
Isn't the ScopeGuard method the way to go (until typeof or decltype |
enters the language)? Have a template function that returns a derived
object which you hold onto using a const reference. The template function
generates an object of the right type, and the const ref allows you to
use it without using the full type name, and calls the object's destructor
when the reference goes out of scope. Maybe a Formula or Format object
could be managed that way.
See http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/ for how it
works.
TB
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Guest
|
Posted: Thu Oct 30, 2003 4:55 am Post subject: Re: Why not obj.Type? |
|
|
The reason that the scope guard way can work is because users don't
have to explicitly call any method in the subclass except the
destructor called by the compiler. (Dismiss() is in the base class and
it does not need to know any information from subclass.)
Say, if we want to allow users to rollback explicitly rather than
waiting for out-of-scope, I don't see how scope guard way will work.
Same to the Formula and io formattor requirement.
When I say:
Matrix t = formula;
cout <
I need the static type information of variable "formula" and "fmt". A
base class reference type obviously won't suffice because the syntax
tree information is lost.
Also, in scope guard way, the Dismiss() is
declared as const while it does have side-effect on the object state.
Sounds contrary to intuition to me.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Torjo Guest
|
Posted: Fri Oct 31, 2003 2:52 pm Post subject: Re: Why not obj.Type? |
|
|
| Quote: | That is,
I can define a typedef named "Type" in the Fmt classes.
And then, a macro can suffice.
#define INFER(name, exp) (exp).Type name(exp)
INFER(f, fmt<<"hello"<< _ << "world");
It does not require any new keyword. All C++ has to do is to allow
refernecing type name from an object, like it does for referencing
static member from an object. Is that too demanding? Of course, the
parser has to be more sophisticated.
|
I really miss such a feature myself.
As I know, C++0x might allow 'auto' keyword to keep any type (at
compile-time), something like:
auto x = function_returning_complex_type();
// use x.
Best,
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|