Topic: C++ Question


Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/07/23
Raw View
Harry J. Kobetitsch <Harry.Kobetitsch@ubsw.com> wrote:

> This is the one that fails:

CODE SNIPPED TO THE ESSENTIAL:

> enum SEVERITY_CODE{FATAL, RECOVERABLE};
>=20
> class PXException
> {
> public:
>    PXException(SEVERITY_CODE sc, char * message)
>            :m_SC(sc), m_message(message) {_logMessage();}
>    PXException(PXException & pxe)
>            :m_SC(pxe.m_SC), m_message(pxe.m_message) {_logMessage();}
> };

> void SomeFunction(void)
> {
>       throw PXException(RECOVERABLE,
>           "Tried to get DataObject through getTree method!");
> }

> _______________________________________________________________
> The warning in VC++ (level 4) comes up at the throw statement.
> It is an error in GNU C++ 2.95.2

> > > warning C4239: nonstandard extension used : 'argument' : conversion
> > > from 'class PXException' to 'class PXException &' A reference that
> > > is not to 'const' cannot be bound to a non-lvalue

Here you can finally see where that diagnostic comes from.

The operand of a throw expression (the object that is thrown) is copied.
(The compiler may omit executing the copy, but it must be legal.

The operand of the throw expression is an rvalue here, as that is what a
direct constructor call gives you.
The copy constructor of PXException is unusal: it can copy only mutable
PXException lvalues, because its parameter type is a _non_-const
reference.

Taken together, the throw expression is ill-formed: It needs to create a
copy of its rvalue operand. But the copy constructor doesn't accept
rvalues.

BTW: Remedy this by making the parameter of the copy ctor a reference to
const.

HTH, J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download StarOffice 5.2 at            http://www.sun.com/staroffice
Participate in OpenOffice at          http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com>
Date: 2000/07/19
Raw View
I've got a question that relates to Visual C++ vs. GNU C++
I have the following C++ code.....
----------------------------------------------------------------
enum SEVERITY_CODE{FATAL, RECOVERABLE};

class PXException
{
public:
  PXException(SEVERITY_CODE sc, char * message)
 :m_SC(sc), m_message(message) {_logMessage();}
  virtual std::string & message() {return m_message;}
  virtual SEVERITY_CODE severityCode() {return m_SC;}

private:
  SEVERITY_CODE m_SC;
  std::string m_message;
  void _logMessage() {
     if (m_SC == FATAL)
          PXlog::writeToLog(m_message);
  }
};

*
*
*
if (aDF)
  return aDF;
else if (aDO)
  throw PXException(RECOVERABLE,
               "Tried to get DataObject through getField method!");
*
*
---------------------------------------------------------------------
In Visual C++, I get no level 3 warnings but I do when I change
to level 4 I get:
warning C4239: nonstandard extension used : 'argument' : conversion
from 'class PXException' to 'class PXException &' A reference that is
not to 'const' cannot be bound to a non-lvalue

In GNU, an error is reported as "initialization of non-const reference
type class PXException&
Can anyone explain what is wrong here?

--
Harry J. Kobetitsch
UBS Warburg
141 West Jackson Blvd.
13th Floor East
Chicago, Illinois 60604
312-554-5866
Harry.Kobetitsch@ubsw.com


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "David Dillard" <ddillard@usa.net>
Date: 2000/07/19
Raw View
"Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com> wrote in message
news:8l2b05$6r4$1@nnrp1.deja.com...
> ---------------------------------------------------------------------
> In Visual C++, I get no level 3 warnings but I do when I change
> to level 4 I get:
> warning C4239: nonstandard extension used : 'argument' : conversion
> from 'class PXException' to 'class PXException &' A reference that is
> not to 'const' cannot be bound to a non-lvalue

You don't say what line you get the warning on.  I tried compiling what you
have here, commenting out the WriteToLog call, and I get no warnings at
level 4 from VC.  I'm using VC6 SP4.


--- David


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com>
Date: 2000/07/21
Raw View
Please disregard the last posting:
This is the one that fails:
____________________________________________________________
PEXception.h
____________________________________________________________

enum SEVERITY_CODE{FATAL, RECOVERABLE};

class PXException
{
public:
   PXException(SEVERITY_CODE sc)
           :m_SC(sc), m_message() {_logMessage();}
   PXException(SEVERITY_CODE sc, std::string & message)
           :m_SC(sc), m_message(message) {_logMessage();}
   PXException(SEVERITY_CODE sc, char * message)
           :m_SC(sc), m_message(message) {_logMessage();}
   PXException(PXException & pxe)
           :m_SC(pxe.m_SC), m_message(pxe.m_message) {_logMessage();}

   virtual std::string & message() {return m_message;}
   virtual SEVERITY_CODE severityCode() {return m_SC;}

private:
   SEVERITY_CODE m_SC;
   std::string m_message;
   void _logMessage() {
      if (m_SC == FATAL)
         PXlog::writeToLog(m_message);
   }
};

____________________________________________________________
SomeFunction.cpp
____________________________________________________________
#include "PXException.h"
void SomeFunction(void)
{
   i = 0;
   if (i != 0)
      cout << i << endl;
   else
      throw PXException(RECOVERABLE,
          "Tried to get DataObject through getTree method!");
}
_______________________________________________________________
The warning in VC++ (level 4) comes up at the throw statement.
It is an error in GNU C++ 2.95.2


In article <oC2d5.702$GQ1.28074@newsread2.prod.itd.earthlink.net>,
  "David Dillard" <ddillard@usa.net> wrote:
> "Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com> wrote in message
> news:8l2b05$6r4$1@nnrp1.deja.com...
> > --------------------------------------------------------------------
-
> > In Visual C++, I get no level 3 warnings but I do when I change
> > to level 4 I get:
> > warning C4239: nonstandard extension used : 'argument' : conversion
> > from 'class PXException' to 'class PXException &' A reference that
is
> > not to 'const' cannot be bound to a non-lvalue
>
> You don't say what line you get the warning on.  I tried compiling
what you
> have here, commenting out the WriteToLog call, and I get no warnings
at
> level 4 from VC.  I'm using VC6 SP4.
>
> --- David
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting
with ]
> [ your news-reader.  If that fails, use mailto:std-
c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---
               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-
c++/faq.html              ]
>
>

--
Harry J. Kobetitsch
UBS Warburg
141 West Jackson Blvd.
13th Floor East
Chicago, Illinois 60604
312-554-5866
Harry.Kobetitsch@ubsw.com


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com>
Date: 2000/07/21
Raw View
Actually, I did miss something:
_______________________________________________________________
PEXception.h
_______________________________________________________________

enum SEVERITY_CODE{FATAL, RECOVERABLE};

class PXException
{
public:
 PXException(SEVERITY_CODE sc)
  :m_SC(sc), m_message() {_logMessage();}
 PXException(SEVERITY_CODE sc, std::string & message)
  :m_SC(sc), m_message(message) {_logMessage();}
 PXException(SEVERITY_CODE sc, char * message)
  :m_SC(sc), m_message(message) {_logMessage();}
 PXException(const PXException & pxe)
  :m_SC(pxe.m_SC), m_message(pxe.m_message) {_logMessage
();}

 virtual std::string & message()
  {return m_message;}

 virtual SEVERITY_CODE severityCode()
  {return m_SC;}

private:
 SEVERITY_CODE m_SC;
 std::string m_message;
 void _logMessage()
 {
  if (m_SC == FATAL)
    PXlog::writeToLog(m_message);
 }
};

__________________________________________________________________
SomeFunction.cpp
__________________________________________________________________
#include PXEception.h
void SomeFunction()
{
   int i = 0;
   if (i != 0)
      return aDT;
   else
      throw PXException(RECOVERABLE,
            "Tried to get DataObject through getTree method!");
}

The warning comes up at the throw line.
I think it has to do with the face that something
in the PEXception class has to be const.

------------------------------------------------------------------
In article <8l2b05$6r4$1@nnrp1.deja.com>,
  "Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com> wrote:
> I've got a question that relates to Visual C++ vs. GNU C++
> I have the following C++ code.....
> ----------------------------------------------------------------
> enum SEVERITY_CODE{FATAL, RECOVERABLE};
>
> class PXException
> {
> public:
>   PXException(SEVERITY_CODE sc, char * message)
>  :m_SC(sc), m_message(message) {_logMessage();}
>   virtual std::string & message() {return m_message;}
>   virtual SEVERITY_CODE severityCode() {return m_SC;}
>
> private:
>   SEVERITY_CODE m_SC;
>   std::string m_message;
>   void _logMessage() {
>      if (m_SC == FATAL)
>           PXlog::writeToLog(m_message);
>   }
> };
>
> *
> *
> *
> if (aDF)
>   return aDF;
> else if (aDO)
>   throw PXException(RECOVERABLE,
>                "Tried to get DataObject through getField method!");
> *
> *
> ---------------------------------------------------------------------
> In Visual C++, I get no level 3 warnings but I do when I change
> to level 4 I get:
> warning C4239: nonstandard extension used : 'argument' : conversion
> from 'class PXException' to 'class PXException &' A reference that is
> not to 'const' cannot be bound to a non-lvalue
>
> In GNU, an error is reported as "initialization of non-const reference
> type class PXException&
> Can anyone explain what is wrong here?
>
> --
> Harry J. Kobetitsch
> UBS Warburg
> 141 West Jackson Blvd.
> 13th Floor East
> Chicago, Illinois 60604
> 312-554-5866
> Harry.Kobetitsch@ubsw.com
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting
with ]
> [ your news-reader.  If that fails, use mailto:std-
c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---
               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-
c++/faq.html              ]
>
>

--
Harry J. Kobetitsch
UBS Warburg
141 West Jackson Blvd.
13th Floor East
Chicago, Illinois 60604
312-554-5866
Harry.Kobetitsch@ubsw.com


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "David Dillard" <ddillard@usa.net>
Date: 2000/07/21
Raw View
"Harry J. Kobetitsch" <Harry.Kobetitsch@ubsw.com> wrote in message
news:8l6tgv$ibm$1@nnrp1.deja.com...
> Please disregard the last posting:
> This is the one that fails:
> The warning in VC++ (level 4) comes up at the throw statement.
> It is an error in GNU C++ 2.95.2

I'm still not getting a warning in VC.  But, a few things come to mind when
looking at the code:

1. Typically, for copy constructors the argument is a const ref, not just a
ref.  So, you might try changing that.  Actually, you can get rid of the
copy constructor completely as the compiler generated one (the Miranda
rights of C++ say that if you do not provide a copy constructor one will be
provided for you) will work fine for your exception class.

2. You might try changing the constructor that takes a string ref as an
argument to taking a const string ref.  Perhaps what's happening is the
compiler is using that constructor before that one that takes a char *
(which could/should be const char *) and using the char * implicit
constructor for a string.  (I don't know that this is happening - its just a
guess.)  Alternatively, you could move the char * constructor before the
string ref constructor in the declaration of the class.


--- David


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]