Re: initialising a vector of strings...

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/28/04


Date: Tue, 28 Dec 2004 20:01:59 +1100


"Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> wrote in message
news:cqj86a$lp3$1@news.hispeed.ch...
| "Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
| news:33283aF3sqh8nU1@individual.net...
| > "Computer Whizz" <old486whizz@hotmail.com> wrote in message
| > news:cqgoc5$h7u$1@newsg3.svr.pol.co.uk...
| > |
| > | "Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
| > | news:3322vaF3q7ls1U1@individual.net...
| > | >
| > | > "Anthony Borla" <ajborla@bigpond.com> wrote in message
| > | > news:GbQyd.86920$K7.55266@news-server.bigpond.net.au...

[snip]

| template<typename T,std::size_t N>
| const char (&arraySize_(T(&)[N]))[N];
| //NB: This function is not implemented, only its signature is used
| // for type deduction: it returns a reference to an array of N chars.
|
| Usage example:
| std::vector<std::string> V( Strings, Strings +
| sizeof(arraySize_(Strings)) );
|
| This is not as neat, but the result is guaranteed to be usable as
| a compile-time constant.

[snip]

Let's hope it makes it into the next revision, as you
stated earlier.

Out of curiosity, I thought I'd try to wrap it up in a
function, as shown below. It gave me a bit of trouble,
because the two compilers (MinGW and BC++B 5.0), were
giving me different results, i.e - worked correctly on
one, but not at all on the other, and visa versa.

In the end, the following example works in MinGW, but
not with my trusty :-) BC++B 5.0, which hasn't got the
best of records when it comes to template bugs :-)

It seems to me that the signature in the declaration
of 'arraySize' only partially matters, since I can
use the function template with very different types.

Again out of curiosity, I tested it with Comeau'
on-line compiler, and it had no complaints about
it either.

I'm still not sure if it is legal though ?
Any thoughts ?

Code:

struct Base {};

namespace arr {
  template<typename T, std::size_t N>
  const char ( &arraySize( T(&)[N]) )[N];

  template<class T, class U>
  std::vector<T> load_from_array( const U& Src )
   {
    return std::vector<T>( Src, Src + sizeof(arraySize( Src )) );
   }
}

int main()
 {
  Base B[ 10 ];
  const char* arr1[] = { "One", "Two", "Three" };
  std::string arr2[] = { "One", "Two", "Three", "Four" };

  std::vector<Base> V1( arr::load_from_array<Base>( B ) );

  std::vector<const char*> V2(
       arr::load_from_array<const char*>( arr1 ) );

  std::vector<std::string> V3(
       arr::load_from_array<std::string>( arr2 ) );

  return 0;
 }

Cheers.
Chris Val



Relevant Pages

  • Re: Steve Mills and top posting
    ... I don't tend to snip ... each separate point should be a new ... > And my signature is separated from the rest of the post by the standard ... keep my sig in all the following posts, helps gets hits on the website, ...
    (rec.juggling)
  • Re: Hello everyone
    ... the body of your message and your signature. ... leave in some attributions as to who posted what and snip out ... Google are evil and are ruining usenet. ...
    (uk.rec.cars.modifications)
  • Re: Deluge!
    ... I'm not a fan of children's novels, but giving away the ending of any story ... in your signature is a very poor form of humour. ...
    (uk.transport.london)
  • Re: Hello everyone
    ... I'm guessing you're new to Usenet. ... It's a "secret" code that means that people who have set up their news client to do so don't have to see your signature. ... It's also a good idea to quote at least a bit of the message you are replying to, type your message under the quoted text, leave in some attributions as to who posted what and snip out parts of the thread that aren't relevant. ...
    (uk.rec.cars.modifications)