473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compilation error in template code from Modern C++ design

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
4 1598
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
#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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
3
by: Ruben Campos | last post by:
Greetings. Some time ago, I had writing a CVector <T, N> class, which implements an algebraic vector of arbitrary both dimension and scalar type. First, I defined the interface for the generic...
3
by: Krivenok Dmitry | last post by:
I writing simple class CmdLine: ..... ..... class CmdLine { ..... ..... public: /// Constructor CmdLine(int argc, char** argv);
2
by: pminkov | last post by:
class A { public: template<class T> void f(); }; template<class T> void A::f<T>() { } When this code is compiled, the following error is produced: error C2768: 'A::f' : illegal use of...
2
by: AlexW | last post by:
Hi, I have error of compilation when I compile below template classes in xcode 2.4.1 on MacOSX10.4.8. This xcode uses GCC 4.0.1 error: there are no arguments to 'SetLeft' that depend on a...
11
by: Fan Yang | last post by:
I'm reading Modern C++ Design, and it is saying "Variable template parameters simply don't exist." But I find VC7.1 & VC8 support this feature.Who can tell me that which is right -_-b Many thanks.
6
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
1
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.