Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compilation error in template code from Modern C++ design

Reply
Thread Tools

Compilation error in template code from Modern C++ design

 
 
dragoncoder
Guest
Posts: n/a
 
      11-08-2006
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.

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
 
 
 
dragoncoder
Guest
Posts: n/a
 
      11-08-2006
> > #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.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      11-08-2006
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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can this, maybe ASP code, be easily replaced with modern code Cal Who ASP .Net 8 01-10-2010 07:39 PM
Compilation error with seperate compilation C__chp C++ 4 02-15-2008 03:57 PM
Query regd. "Modern C++ Design" program. frame C++ 1 12-11-2006 12:30 PM
Modern C++ Design Michael C++ 4 11-30-2004 08:38 PM
Modern CPP Design related LRS Kumar C++ 2 06-26-2004 02:35 PM



Advertisments