Compilation error in template code from Modern C++ design

D

dragoncoder

Hi all,

I am reading Modern C++ design by Andrei Alexandrescu. In the
Techniques chapter I came across this code for using a run time
assertion. Here is the code.

#include <cassert>

template <class To, class From>
To safe_reinterpret_cast ( From from)
{
std::assert ( sizeof (From) <= sizeof (To) );
return reinterpret_cast <To> (from);
}

int main()
{
char c = 'a';
char* pc = safe_reinterpret_cast <char*> (c);
}

I don't see any problem with the code, but when I compile this I get
the following error.

=> g++ p4.cxx
p4.cxx: In function `To safe_reinterpret_cast(From)':
p4.cxx:6: syntax error before `static_cast'

Can someone please explain why the error is coming and where is the
static_cast involved here ?

Thanks in advance.
 
M

mlimber

dragoncoder said:
Hi all,

I am reading Modern C++ design by Andrei Alexandrescu. In the
Techniques chapter I came across this code for using a run time
assertion. Here is the code.

#include <cassert>

template <class To, class From>
To safe_reinterpret_cast ( From from)
{
std::assert ( sizeof (From) <= sizeof (To) );

That's not what my edition says. assert is a macro, not a function.
Drop the std:: qualification.
return reinterpret_cast <To> (from);
}

int main()
{
char c = 'a';
char* pc = safe_reinterpret_cast <char*> (c);
}

I don't see any problem with the code, but when I compile this I get
the following error.

=> g++ p4.cxx
p4.cxx: In function `To safe_reinterpret_cast(From)':
p4.cxx:6: syntax error before `static_cast'

Can someone please explain why the error is coming and where is the
static_cast involved here ?

Thanks in advance.

Cheers! --M
 
D

dragoncoder

#include said:
That's not what my edition says. assert is a macro, not a function.
Drop the std:: qualification.

Oh, that was a terrible mistake and dropping the std:: qualification
compiles the. I always get confused among the macros and functions.

Anyway, any idea on why the error is saying something related to
static_cast, just out of curiosity ?

Thanks again.
 
M

mlimber

dragoncoder said:
Oh, that was a terrible mistake and dropping the std:: qualification
compiles the. I always get confused among the macros and functions.

Anyway, any idea on why the error is saying something related to
static_cast, just out of curiosity ?

The compiler just got confused and gave you the best diagnostic it
could. If you think that's bad, wait until you see non-sensical error
messages due to a missing semi-colon or closing brace. I've seen
hundreds of errors go away with the addition of a crucial punctuation
mark.

Cheers! --M
 
G

Gianni Mariani

mlimber said:
The compiler just got confused and gave you the best diagnostic it
could. If you think that's bad, wait until you see non-sensical error
messages due to a missing semi-colon or closing brace. I've seen
hundreds of errors go away with the addition of a crucial punctuation
mark.

That's why I usually fix the first error in a compile output and if I
don't grok the second error I compile again. The compiler should always
give you the first (syntax) error in the file (if it has any errors that
is).
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top