Template Stringizing-How should it work?

S

Steven T. Hatton

I'm serious about this folks! I really want to see the CPP obviated.
People who have never used tools such as JBuilder may not fully appreciate
the advantages they provide. This isn't about pointer safety, or making
sure you aren't blowing the bounds of an array. Imagine you could select a
namespace, enter a command with a new name as an argument, and your IDE
would rename every identifier qualified with that namespace to the new
name. Not only that, but it would fix every reference throughout the code
base so that it referred to the new namespace. The JBuilder creators call
that "refactoring" (using packages rather than namespaces). It isn't what
I mean by the term "refactor". To me, that is only a minor proceedure
performed as part of refactoring. But that's not what I want to discuss.
I'm just using it as an example of the many different capabilities Java
IDEs have.

I sincerely believe the CPP is the biggest reason C++ IDEs can't do these
kind of things. I'll add the observation that, if it's hard to build a
tool to navigate and manipulate source code, it's probably hard for a
programmer to do the same. Stroustrup's suggested approach to doing away
with the CPP is to find ways to accomplish the things people use the CPP
for as easily in native C++. You guys probably think I ignore everything
you tell me you use the CPP for. Well, that isn't the case. And I use the
CPP directly or indirectly as much, or more than a lot of you do. See for
example: http://doc.trolltech.com/3.3/moc.html#moc

There are three different areas of use that I have been able to identify for
the CPP. 1) #inclusion, 2) conditional compilation 3) stringizing program
elements. Right now I want to talk about the third of these. I just
discovered that it is possible to pass the equivalent of a string literal
to a template like this:

ISO/IEC 14882 §14.3.2
[Note: A string literal (2.13.4) does not satisfy the requirements of any of
these categories and thus is not an
acceptable template-argument. [Example:
template<class T, char* p> class X {
// ...
X();
X(const char* q) { /* ... */ }
};
// error: string literal
as template-argument
X<int,"Studebaker"> x1;
char p[] = "Vivisectionist";
// OK
X<int,p> x2;
--end example] --end note]

I know this doesn't provide what the macro(#) provides. But it may be a
place to start. Could templates be used for the same kinds of things you
currently used the CPP # parameter for? What would they have to provide?
I'm not looking for the most esoteric example you can invent. I'm asking
about something that would provide 90% coverage.

Here's my first thought. Pass the name of a class to a template, and have
it converted to some kind of inner class with string representations of the
member variables and member functions. That might be useful for stuff like
introspection.
 
J

Jeff Flinn

Steven T. Hatton said:
I'm serious about this folks! I really want to see the CPP obviated.
....

There are three different areas of use that I have been able to identify for
the CPP. 1) #inclusion, 2) conditional compilation 3) stringizing program
elements.

I doubt life is this simple. Can you survey boost usage of PP (BOOSTPP
aside), and say that all uses of PP fall in these categories? Function
signature arity generation comes to mind.

Jeff F
 
S

Steven T. Hatton

Jeff said:
I doubt life is this simple. Can you survey boost usage of PP (BOOSTPP
aside), and say that all uses of PP fall in these categories? Function
signature arity generation comes to mind.

Jeff F

It doesn't really matter if I did not cover all usage. I didn't intend that
list to be comprehensive. What I covered are the uses of the CPP that I see
most in code I read. If there are issues that remain, they should be
addressed as well. I suspect some of the uses for the CPP are simply way
of doingthings that /could/ be done, and done well within C++, but the
person writing the code didn't put forth the effort to find that better
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top