Topic: Auto array index naming
Author: slamb@easystreet.com ("Stephen Lamb")
Date: Thu, 30 Sep 2004 04:30:26 GMT Raw View
"Ben Strasser" <ben04_01@freenet.de> wrote in message
news:2oket8Fbq3mgU1@uni-berlin.de...
> Laurie Cheers wrote:
> > It would be really useful to be able to name array indexes
> > automatically as they're declared. For example:
> >
> > string s_StringTable[] = {
> > [STRING_TEST] "A Test String",
> > [STRING_HELLO] "Hello",
> > [STRING_GOODBYE] "Goodbye"
> > };
> > out << s_StringTable[STRING_HELLO];
> >
> >
> > At the moment I'm doing this with:
> > enum StringTableID
> > {
> > STRING_TEST = 0,
> > STRING_HELLO,
> > STRING_GOODBYE
> > }
> > string s_StringTable[] = {
> > "A Test String",
> > "Hello",
> > "Goodbye"
> > };
> >
> > ..which, needless to say, is a maintenance nightmare.
If you have a lot of strings & ids then maybe this solution is a little
better, it at least keeps the ids with the strings. Anybody know of a
better solution? This technique was actually used at the last place I
worked as a cross platform string table.
// Some source file
#define GENERATE_ENUM
#include "header.hpp"
#undef GENERATE_ENUM
#include "header.hpp"
void foo()
{
const std::string & s = s_StringTable[STRING_HELLO];
std::cout << s;
}
// header.hpp
#if defined(GENERATE_ENUM)
#define CHOOSE_ID_OR_STRING(id, str) id
#else
#define CHOOSE_ID_OR_STRING(id, str) str
#endif
#if defined(GENERATE_ENUM)
enum StringTableID
#else
std::string s_StringTable[] =
#endif
{
CHOOSE_ID_OR_STRING(STRING_TEST, "A Test String"),
CHOOSE_ID_OR_STRING(STRING_HELLO, "Hello"),
CHOOSE_ID_OR_STRING(STRING_GOODBYE, "Goodbye")
};
#undef CHOOSE_ID_OR_STRING
---
[ 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: kuyper@wizard.net (James Kuyper)
Date: Sun, 22 Aug 2004 05:38:25 GMT Raw View
laurie.cheers@btinternet.com (Laurie Cheers) wrote in message news:<c26006e0.0408200601.61642f30@posting.google.com>...
..
> string s_StringArray[NUM_STRINGS] = {
> STRING_TEST: "A Test String",
> STRING_GOODBYE: "Goodbye",
> STRING_FOO: STRING_HELLO: "Hello",
> default: "!!String undefined!!"
> }
C99 allows something like this:
string s_StringArray[NUM_STRINGS] = {
[STRING_TEST]= "A Test String",
[STRING_GOODBYE]= "Goodbye",
[STRING_FOO]= "Hello",
[STRING_HELLO]= "Hello",
[STRING_INVALID]= "!!String undefined!!"
}
This might be a candidate for inclusion in the next version of C++,
though I can forsee all kinds of nasty interactions with C++-specific
issues that didn't matter for C99.
---
[ 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: nagle@animats.com (John Nagle)
Date: Mon, 23 Aug 2004 00:47:26 GMT Raw View
Laurie Cheers wrote:
> It would be really useful to be able to name array indexes
> automatically as they're declared. For example:
This is a special case of a more general problem.
C++ inherited C's mechanism for initializing arrays,
but didn't generalize it to allow initialization of
C++ collections.
It would be useful to be able to write
using namespace std;
const vector<string> colors(
{ string("Red"), string("Blue"), string("Green") }) ;
John Nagle
Animats
---
[ 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: ben04_01@freenet.de (Ben Strasser)
Date: Sat, 21 Aug 2004 16:38:52 GMT Raw View
Laurie Cheers wrote:
> It would be really useful to be able to name array indexes
> automatically as they're declared. For example:
>
> string s_StringTable[] = {
> [STRING_TEST] "A Test String",
> [STRING_HELLO] "Hello",
> [STRING_GOODBYE] "Goodbye"
> };
> out << s_StringTable[STRING_HELLO];
>
>
> At the moment I'm doing this with:
> enum StringTableID
> {
> STRING_TEST = 0,
> STRING_HELLO,
> STRING_GOODBYE
> }
> string s_StringTable[] = {
> "A Test String",
> "Hello",
> "Goodbye"
> };
>
> ..which, needless to say, is a maintenance nightmare.
Why not:
string test="A Test String";
string hello="Hello";
string goodbye="Goodbye";
cout<<test;
void foo(string&);
foo(test);
I don't see where you can not use that approach where what you suggest
would work (or better would make sence)
---
[ 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: jdibling@computer.org (John Dibling)
Date: Sat, 21 Aug 2004 16:41:02 GMT Raw View
On Thu, 19 Aug 2004 18:39:21 GMT, laurie.cheers@btinternet.com (Laurie
Cheers) wrote:
>It would be really useful to be able to name array indexes
>automatically as they're declared. For example:
>
>string s_StringTable[] = {
> [STRING_TEST] "A Test String",
> [STRING_HELLO] "Hello",
> [STRING_GOODBYE] "Goodbye"
>};
>out << s_StringTable[STRING_HELLO];
Its a maintenance nightmare as you say because you are trying to keep
two collections of information in synch -- one being an array of
strings, the other being a loose bag of magic numbers. Magic numbers
are trouble.
Why not merge the magic numbers with the actual goodies?
static const char STRING_TEST [] = "A Test String";
static const char STRING_HELLO [] = "Hello";
static const char STRING_GOODBYE [] = "Goodbye";
- John
---
[ 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: laurie.cheers@btinternet.com (Laurie Cheers)
Date: Sat, 21 Aug 2004 16:47:28 GMT Raw View
v.Abazarov@comAcast.net (Victor Bazarov) wrote in message news:<bq7Vc.106$Ae.56@newsread1.dllstx09.us.to.verio.net>...
> Laurie Cheers wrote:
> > It would be really useful to be able to name array indexes
> > automatically as they're declared. For example:
> >
> > string s_StringTable[] = {
> > [STRING_TEST] "A Test String",
> > [STRING_HELLO] "Hello",
> > [STRING_GOODBYE] "Goodbye"
> > };
> > out << s_StringTable[STRING_HELLO];
>
> Wouldn't that be unnecessary if you had, say, a way to initialise
> a map:
>
> typedef std::map<int,std::string> str_table;
>
> str_table s_StringTable = { { STRING_TEST, "A Test String" }
> , { STRING_HELLO, "Hello" }
> , { STRING_GOODBYE, "Goodbye" } };
>
> out << s_StringTable[STRING_HELLO];
>
> ? I know, I know, std::map is not an aggregate, it cannot be
> initialised that way. But wouldn't that be nice?
Indeed, that would be nice. Maybe std::map should be given a
constructor that supports an arbitrary number of arguments,
printf style?
str_table s_StringTable (
str_entry( STRING_TEST, "A Test String" ),
str_entry( STRING_HELLO, "Hello" ),
str_entry( STRING_GOODBYE, "Goodbye" ),
);
I think you read my suggestion backwards, though; my original
intent was that this would be a way to automatically _declare_
something like 'const unsigned STRING_TEST = 0', etc, during array
initialisation. Essentially, declaration of an enum and an array
merged into one.
The map version would presumably require a set of previously
defined constants.
On reflection, that's actually a more useful approach - it lets
multiple arrays use the same set of index values, and it makes it
trivial to leave entries undefined, or declared out of order...
I like that.
I simply want to treat this as a static array/vector, though,
not a map: there's no reason why a search needs to take O(log n)
time.
So, I revise my suggestion. Wouldn't it be nice if an array
declaration, instead of automatically filling from 0 onwards,
could be given a const int for each value to show what index to
put that value in? It might be nice to allow multiple indices,
showing that the value appears more than once in the table,
like multiple cases in a switch statement.
Also like a switch statement, it would be useful to support a
"default" that handles entries which haven't been otherwise
defined. For example:
enum StringIndexID {
STRING_INVALID = 0,
STRING_TEST,
STRING_HELLO,
STRING_GOODBYE,
STRING_FOO,
STRING_PADDING,
NUM_STRINGS
};
string s_StringArray[NUM_STRINGS] = {
STRING_TEST: "A Test String",
STRING_GOODBYE: "Goodbye",
STRING_FOO: STRING_HELLO: "Hello",
default: "!!String undefined!!"
}
In case it's not clear, this array would be initialised to:
"!!String undefined!!"
"A Test String"
"Hello"
"Goodbye"
"Hello"
"!!String undefined!!"
--
Laurie Cheers
---
[ 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: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Thu, 19 Aug 2004 20:18:00 GMT Raw View
Laurie Cheers wrote:
> It would be really useful to be able to name array indexes
> automatically as they're declared. For example:
>
> string s_StringTable[] = {
> [STRING_TEST] "A Test String",
> [STRING_HELLO] "Hello",
> [STRING_GOODBYE] "Goodbye"
> };
> out << s_StringTable[STRING_HELLO];
>
>
> At the moment I'm doing this with:
> enum StringTableID
> {
> STRING_TEST = 0,
> STRING_HELLO,
> STRING_GOODBYE
> }
> string s_StringTable[] = {
> "A Test String",
> "Hello",
> "Goodbye"
> };
>
> ..which, needless to say, is a maintenance nightmare.
Wouldn't that be unnecessary if you had, say, a way to initialise
a map:
typedef std::map<int,std::string> str_table;
str_table s_StringTable = { { STRING_TEST, "A Test String" }
, { STRING_HELLO, "Hello" }
, { STRING_GOODBYE, "Goodbye" } };
out << s_StringTable[STRING_HELLO];
? I know, I know, std::map is not an aggregate, it cannot be
initialised that way. But wouldn't that be nice?
Just a thought.
Victor
---
[ 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: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Fri, 20 Aug 2004 16:36:13 GMT Raw View
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:bq7Vc.106$Ae.56@newsread1.dllstx09.us.to.verio.net...
| Laurie Cheers wrote:
| > It would be really useful to be able to name array indexes
| > automatically as they're declared. For example:
| >
| > string s_StringTable[] = {
| > [STRING_TEST] "A Test String",
| > [STRING_HELLO] "Hello",
| > [STRING_GOODBYE] "Goodbye"
| > };
| > out << s_StringTable[STRING_HELLO];
| Wouldn't that be unnecessary if you had, say, a way to initialise
| a map:
|
| typedef std::map<int,std::string> str_table;
|
| str_table s_StringTable = { { STRING_TEST, "A Test String" }
| , { STRING_HELLO, "Hello" }
| , { STRING_GOODBYE, "Goodbye" } };
|
| out << s_StringTable[STRING_HELLO];
|
| ? I know, I know, std::map is not an aggregate, it cannot be
| initialised that way. But wouldn't that be nice?
Apart from the fact that you have to make an enumeration for the constants, my new library at boost (Boost.Assign) allows you to say
str_table StringTable = map_list_of( STRING_TEST, "A Test String")
(STRING_HELLO, "Hello")
(STRING_GOODBYE,"Goodbye");
It will be in the soon-to.appear next release
br
Thortsten
---
[ 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: laurie.cheers@btinternet.com (Laurie Cheers)
Date: Thu, 19 Aug 2004 18:39:21 GMT Raw View
It would be really useful to be able to name array indexes
automatically as they're declared. For example:
string s_StringTable[] = {
[STRING_TEST] "A Test String",
[STRING_HELLO] "Hello",
[STRING_GOODBYE] "Goodbye"
};
out << s_StringTable[STRING_HELLO];
At the moment I'm doing this with:
enum StringTableID
{
STRING_TEST = 0,
STRING_HELLO,
STRING_GOODBYE
}
string s_StringTable[] = {
"A Test String",
"Hello",
"Goodbye"
};
..which, needless to say, is a maintenance nightmare.
---
[ 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: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Mon, 23 Aug 2004 15:55:02 GMT Raw View
"John Nagle" <nagle@animats.com> wrote in message news:w26Wc.6616$QJ3.4801@newssvr21.news.prodigy.com...
| Laurie Cheers wrote:
|
| > It would be really useful to be able to name array indexes
| > automatically as they're declared. For example:
|
| This is a special case of a more general problem.
| C++ inherited C's mechanism for initializing arrays,
| but didn't generalize it to allow initialization of
| C++ collections.
|
| It would be useful to be able to write
|
| using namespace std;
| const vector<string> colors(
| { string("Red"), string("Blue"), string("Green") }) ;
I think you might find boost.assign helpful then:
const vector<string> colors = boost::assign::list_of( "Red" )( "Blue")("Green");
br
Thorsten
---
[ 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: laurie.cheers@btinternet.com (Laurie Cheers)
Date: Tue, 24 Aug 2004 02:40:35 GMT Raw View
jdibling@computer.org (John Dibling) wrote in message news:<ur8ai0hbui6nif7urdtvv169bbjd1osvf1@4ax.com>...
> Its a maintenance nightmare as you say because you are trying to keep
> two collections of information in synch -- one being an array of
> strings, the other being a loose bag of magic numbers. Magic numbers
> are trouble.
>
> Why not merge the magic numbers with the actual goodies?
>
> static const char STRING_TEST [] = "A Test String";
> static const char STRING_HELLO [] = "Hello";
> static const char STRING_GOODBYE [] = "Goodbye";
>
> - John
If there's a portable way to refer to these, that would certainly work.
(I'm currently sending the magic numbers as messages across a network.)
--
Laurie Cheers
---
[ 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 ]