By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Compilation error in template code from Modern C++ design

P: n/a
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.

Nov 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
dragoncoder wrote:
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

Nov 8 '06 #2

P: n/a
#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.
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.

Nov 8 '06 #3

P: n/a
dragoncoder wrote:
#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.

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

Nov 8 '06 #4

P: n/a
mlimber wrote:
dragoncoder wrote:
>>>#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.
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.
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).
Nov 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.