Click here to Skip to main content
15,921,837 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: is it technically right to refer programs written for MS-DOS as 16 bit Pin
Jayapal Chandran29-Oct-10 22:38
Jayapal Chandran29-Oct-10 22:38 
Questionsizeof string array in for loop? Pin
ALLERSLIT28-Oct-10 4:40
ALLERSLIT28-Oct-10 4:40 
AnswerRe: sizeof string array in for loop? Pin
Chris Losinger28-Oct-10 4:56
professionalChris Losinger28-Oct-10 4:56 
GeneralRe: sizeof string array in for loop? Pin
ALLERSLIT28-Oct-10 5:23
ALLERSLIT28-Oct-10 5:23 
AnswerRe: sizeof string array in for loop? Pin
Sauro Viti28-Oct-10 5:23
professionalSauro Viti28-Oct-10 5:23 
GeneralRe: sizeof string array in for loop? Pin
ALLERSLIT28-Oct-10 5:29
ALLERSLIT28-Oct-10 5:29 
GeneralRe: sizeof string array in for loop? Pin
Sauro Viti28-Oct-10 6:08
professionalSauro Viti28-Oct-10 6:08 
AnswerRe: sizeof string array in for loop? Pin
Aescleal28-Oct-10 23:29
Aescleal28-Oct-10 23:29 
What's wrong with asking the compiler how big the array is - it knows, it bunged the thing on the stack for you:

template <typename T, std::size_t N>
std::size_t sizeof_array( T (&)[N] )
{
	return N;
}


Most decent compilers (VC++2003, 2005, 2008 and 2010, gcc 3.3 onwards) will inline that to a constant. You can extend it a bit...

template <typename T, std::size_t N>
T *beginning_of_array( T (&a)[N] )
{
	return a;
}

template <typename T, std::size_t N>
T *end_of_array( T (&a)[N] )
{
	return &a[ N ];
}


which means you can get away from using indexes entirely and start using algorithms to do the sort of thing you're doing in your example:

int main()
{
	std::string a[] = { "One", "Two", "Three" };

	std::copy( beginning_of_array( a ), end_of_array( a ), std::ostream_iterator<std::string>( std::cout, "\n" ) );
}


From there you're not far away from using overloaded functions to come up with a generic way of handling different aggregates of things.

Cheers,

Ash
QuestionProblem with ScrollBar(MFC VC6) Pin
ashtwin28-Oct-10 1:50
ashtwin28-Oct-10 1:50 
AnswerRe: Problem with ScrollBar(MFC VC6) Pin
Richard MacCutchan28-Oct-10 2:58
mveRichard MacCutchan28-Oct-10 2:58 
GeneralRe: Problem with ScrollBar(MFC VC6) Pin
ashtwin28-Oct-10 21:23
ashtwin28-Oct-10 21:23 
GeneralRe: Problem with ScrollBar(MFC VC6) Pin
Richard MacCutchan28-Oct-10 23:05
mveRichard MacCutchan28-Oct-10 23:05 
QuestionEncoderParameter in GDIPLUS Pin
002comp28-Oct-10 0:40
002comp28-Oct-10 0:40 
QuestionUse of Non-Unicode DLL in Unicode project Pin
chevu27-Oct-10 21:45
chevu27-Oct-10 21:45 
AnswerRe: Use of Non-Unicode DLL in Unicode project [modified] Pin
Aescleal27-Oct-10 22:07
Aescleal27-Oct-10 22:07 
AnswerRe: Use of Non-Unicode DLL in Unicode project Pin
Alain Rist27-Oct-10 22:41
Alain Rist27-Oct-10 22:41 
QuestionWindows 7 VS 2008 fatal error LNK1168: cannot open file.exe for writing Pin
transoft27-Oct-10 17:16
transoft27-Oct-10 17:16 
AnswerRe: Windows 7 VS 2008 fatal error LNK1168: cannot open file.exe for writing Pin
Luc Pattyn27-Oct-10 17:21
sitebuilderLuc Pattyn27-Oct-10 17:21 
GeneralRe: Windows 7 VS 2008 fatal error LNK1168: cannot open file.exe for writing Pin
transoft27-Oct-10 17:28
transoft27-Oct-10 17:28 
AnswerRe: Windows 7 VS 2008 fatal error LNK1168: cannot open file.exe for writing Pin
Luc Pattyn27-Oct-10 17:57
sitebuilderLuc Pattyn27-Oct-10 17:57 
Questionhow to simultaneously show 2 dialogs Pin
Krauze27-Oct-10 16:53
Krauze27-Oct-10 16:53 
AnswerRe: how to simultaneously show 2 dialogs Pin
transoft27-Oct-10 17:19
transoft27-Oct-10 17:19 
AnswerRe: how to simultaneously show 2 dialogs Pin
Luc Pattyn27-Oct-10 17:24
sitebuilderLuc Pattyn27-Oct-10 17:24 
GeneralRe: how to simultaneously show 2 dialogs Pin
Stephen Hewitt27-Oct-10 18:47
Stephen Hewitt27-Oct-10 18:47 
GeneralRe: how to simultaneously show 2 dialogs Pin
Luc Pattyn28-Oct-10 1:35
sitebuilderLuc Pattyn28-Oct-10 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.