How do I write this function only once using templates?

S

Siegfried Heintze

How do I use a template to write these friend functions once? The are nearly
identical. Also, why does wcout << L'A'; print 65 instead of a character?
Thanks,
Siegfried

struct DVDDeltaTime{
// constructors go here...
friend std::eek:stream & operator<<(std::eek:stream &os, const DVDDeltaTime&
dt ){
return os
<< ' ' << std::setw(2) << std::setfill('0') << dt.m_nTitles
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nHours
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nMinutes
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nSeconds
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nFrames;
}
friend std::wostream & operator<<(std::wostream &os, const DVDDeltaTime&
dt ){
return os
<< L" " << std::setw(2) << std::setfill(L'0') << dt.m_nTitles
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nHours
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nMinutes
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nSeconds
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nFrames ;
}
private:
unsigned m_nTitles:8, m_nHours:6, m_nMinutes:6, m_nSeconds:6,
m_nFrames:6;
}
 
J

Jonathan Turkanis

Siegfried Heintze said:
How do I use a template to write these friend functions once? The are nearly
identical. Also, why does wcout << L'A'; print 65 instead of a character?
Thanks,
Siegfried

template<typename Ch, typename Tr>
friend std::basic_ostream<Ch, Tr>& operator<<
(std::basic_ostream<Ch, Tr>& out, DVDDeltaTime) { /* ... */ }

Be aware that not all compilers are happy with this.

Jonathan
 
B

bartek

(...)
Also, why does wcout << L'A'; print 65 instead of a
character?

It's actually off topic... but anyway...
This is a "MSVC++ Specific Behaviour(TM)" - they don't sport wchar_t as a
unique type, but rather a typedef from unsigned short. poof!

Cheers,
b
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top