Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serialization template classes

3 views
Skip to first unread message

Johannes Bauer

unread,
Jun 3, 2009, 11:26:54 AM6/3/09
to
Hello group,

I've written a serialization class which serializes arbitrary objects to
by "Block" representation. A template exists for POD objects, as do some
specializations, like:

template<> class Serializer<std::string> {
public:
static void Serialize(const std::string &Input, Block &Output) {
Serializer<unsigned int>::Serialize(Input.size(), Output);
Output.Increment(Input.size());
std::copy(Input.begin(), Input.end(), Output.endptr() - Input.size());
}
[...]

Those all work fine. Now I wanted to introduce a template that could
reduce a map which contains arbitrary types:

template<typename T1, typename T2> class Serializer< std::map<T1, T2> > {
public:
static void Serialize(const std::map<T1, T2> &Input, Block &Output) {
Serializer<unsigned int>::Serialize(Input.size(), Output);
std::map<T1, T2>::const_iterator i;
for (i = Input.begin(); i != Input.end(); i++) {
Serializer<T1>::Serialize(i->first, Output);
Serializer<T2>::Serialize(i->second, Output);
}
}

The compiler however now complains with a *very* weird error:

Common/Serializer.hpp: In static member function ‘static void
Serializer<std::map<T1, T2, std::less<_Key>,
std::allocator<std::pair<const _Key, _Tp> > > >::Serialize(const
std::map<T1, T2, std::less<_Key>, std::allocator<std::pair<const _Key,
_Tp> > >&, Block&)’:
Common/Serializer.hpp:61: error: expected `;' before ‘i’
Common/Serializer.hpp:62: error: ‘i’ was not declared in this scope

What am I doing wrong?

Kind regards,
Johannes

--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$5402...@news.sunrise.ch>

Ron AF Greve

unread,
Jun 3, 2009, 11:32:19 AM6/3/09
to
Hi,

I usually try putting typename in front of declararions with the g++
compiler

typename std::map<T1, T2>::const_iterator i;

Not sure if that is the problem but you could give it a try.

Regards, Ron AF Greve

http://informationsuperhighway.eu

"Johannes Bauer" <dfnson...@gmx.de> wrote in message
news:78nj1sF...@mid.dfncis.de...

> verl�sterung von Gott, Bibel und mir und bewusster Blasphemie."
> -- Prophet und Vision�r Hans Joss aka HJP in de.sci.physik
> <48d8bf1d$0$7510$5402...@news.sunrise.ch>


Johannes Bauer

unread,
Jun 3, 2009, 11:35:58 AM6/3/09
to
Hi Ron,

Ron AF Greve schrieb:

> I usually try putting typename in front of declararions with the g++
> compiler
>
> typename std::map<T1, T2>::const_iterator i;
>
> Not sure if that is the problem but you could give it a try.

Indeed! Now what the heck did just happen there? Why do I need the
"typename" keyword - is this a compiler problem or am I misunderstanding
part of the language?

Kind regards,
Johannes

--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,

verlᅵsterung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Visionᅵr Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$5402...@news.sunrise.ch>

Maxim Yegorushkin

unread,
Jun 3, 2009, 12:06:07 PM6/3/09
to
On Jun 3, 4:35 pm, Johannes Bauer <dfnsonfsdu...@gmx.de> wrote:
> Hi Ron,
>
> Ron AF Greve schrieb:
>
> > I usually try putting typename in front of declararions with the g++
> > compiler
>
> > typename std::map<T1, T2>::const_iterator i;
>
> > Not sure if that is the problem but you could give it a try.
>
> Indeed! Now what the heck did just happen there? Why do I need the
> "typename" keyword - is this a compiler problem or am I misunderstanding
> part of the language?

Please see here:
http://www.comeaucomputing.com/techtalk/#typename
http://ricin.lfsnal.org/books/oreilly/books/cplsian/cplsian-CHP-7-SECT-8.html

However, your code is still not quite right.

The reason is that std::map<> has three template arguments, not two.
Some (older) compilers may accept your code and assume the default
value for the third template argument.

--
Max

Bart van Ingen Schenau

unread,
Jun 3, 2009, 1:52:28 PM6/3/09
to
Johannes Bauer wrote:

> Hi Ron,
>
> Ron AF Greve schrieb:
>
>> I usually try putting typename in front of declararions with the g++
>> compiler
>>
>> typename std::map<T1, T2>::const_iterator i;
>>
>> Not sure if that is the problem but you could give it a try.
>
> Indeed! Now what the heck did just happen there? Why do I need the
> "typename" keyword - is this a compiler problem or am I
> misunderstanding part of the language?

This is a part of the language.
The name "std::map<T1, T2>::const_iterator" is a so-called dependent
name, because the exact meaning of std::map<T1, T2>::const_iterator
depends on the values of the template parameters T1 and T2.
The problem with dependent names is that the compiler can't decide if it
is supposed to refer to a type or to a variable, and the default choice
then become to interpret the name as referring to a variable.
If the default choice is wrong, you must instruct the compiler to assume
it will be a type name with the typename keyword.

>
> Kind regards,
> Johannes
>

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

co...@mailvault.com

unread,
Jun 3, 2009, 2:08:09 PM6/3/09
to

Wikipedia has a page on serialization that has a C++
section -- http://en.wikipedia.org/wiki/Serialization.

There's a comparison between Boost Serialization and
the C++ Middleware Writer here --
http://webEbenezer.net/comparison.html.

The C++ Middleware Writer, on line since 2002,
was the first on line, C++ code generator.


Brian Wood
Ebenezer Enterprises
www.webEbenezer.net

I recommend the articles on social topics by professor
Jonathan Katz -- http://wuphys.wustl.edu/~katz.

0 new messages