Re: initialising a vector of strings...
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/28/04
- Next message: Wouter de Kort: "Re: Using std::vector on a base/derived class"
- Previous message: Alwyn: "Re: How to read the C++ programming language."
- In reply to: Ivan Vecerina: "Re: initialising a vector of strings..."
- Next in thread: B. v Ingen Schenau: "Re: initialising a vector of strings..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Wouter de Kort: "Re: Using std::vector on a base/derived class"
- Previous message: Alwyn: "Re: How to read the C++ programming language."
- In reply to: Ivan Vecerina: "Re: initialising a vector of strings..."
- Next in thread: B. v Ingen Schenau: "Re: initialising a vector of strings..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|