Topic: const double cos(const double)


Author: Jim McKelvey <mckelvey@bean.jpl.nasa.gov>
Date: 1998/08/04
Raw View
AllanW@my-dejanews.com wrote:
>
> In article <35C1FF44.5ACD@bean.jpl.nasa.gov>,
>   mckelvey@bean.jpl.nasa.gov wrote:
> > I notice that math routines are sometimes prototyped in C headers like:
AllanW@my-dejanews.com wrote:
>
> In article <35C1FF44.5ACD@bean.jpl.nasa.gov>,
>   mckelvey@bean.jpl.nasa.gov wrote:
> > I notice that math routines are sometimes prototyped in C headers like:
> >
> > const double cos(const double);
> > ^^^^^
>
> I'm curious: what compiler does this?
>

Most of the math functions on my NeXT (3.3) are prototyped that way.
That UNIX is based on BSD, and the compiler is derived from gcc.

> > Does this have any special meaning in C++?
>
> No; the value isn't an lvalue, so it can't be modified anyway. It doesn't
> have any special meaning in C, either, last time I looked (a few years
> now -- please correct me if it's changed).

No, it appears to be legal, but pointless. I have a theory that some
older compilers used it as an indicator that the function was
constant-for-constant-input (is that the same as idempotent?).

So in:

for (i = 0; i < 3; i++)
{
    x[i] = i + cos(0.1);
}

the call to cos(0.1) could be hoisted out of the loop.
---
[ 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: AllanW@my-dejanews.com
Date: 1998/08/04
Raw View
In article <haberg-0208981557180001@sl82.modempool.kth.se>,
  haberg@REMOVE.matematik.su.se (Hans Aberg) wrote:
> In article <6q1itu$o90$1@mulga.cs.mu.OZ.AU>, fjh@cs.mu.OZ.AU (Fergus
> Henderson) wrote:
> >Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:
> >>const double cos(const double);
> >>^^^^^
> >>
> >>Does this have any special meaning in C++?
> >
> >Not in standard C++.
>
>   My impression was that this would mean that the returned value cannot
> change. For example:
>     const char* hello = "Hello";
>     const char* name() { return hello; }
> Then the next user will know that this is a value that cannot be changed.

This isn't really the same thing. Function name() returns a pointer to
a char that is const. But the cos() function above returns data which
is already const. To be comparable, function name() would have to be
declared
    const char* const name2() { return hello; }
As before, the first const specifies that the object pointed to (in this
case, the letter 'H') cannot be changed. The second const specifies that
the pointer returned cannot be changed either. The question is, does
that second const have any meaning?

(To rephrase the whole thing more clearly, let's use a typedef.
    typedef const char* Text;
    Text hello = "Hello";
    Text name() { return hello; }
    const Text name2() { return hello; }
This is equivalent, and again the question is if the last "const" has
any effect.)

>   For doubles one might have:
>     const double pi = 2 * asin(1.0);
>     const double Pi() { return pi; }
> Then, with
>     void f(double&);
>     void g(const double&);
> one gets
>     main() {
>         f(Pi());   // Error: Cannot convert const double -> double&
>         g(Pi());   // OK: Can convert const double -> const double&

A more meaningful test case might be a non-const copy constructor:
    class NC_Copy {
    public:
        NC_Copy()         {} // Default constructor
        NC_Copy(NC_Copy&) {} // Note argument isn't const
    };
    NC_Copy copy1;
    const NC_Copy test() { return copy1; }
    int main(int,char**) {
        NC_Copy copy2(test()); // Tries to copy a const copy of copy1?
        return 0;
    }
This might be illegal because test() returns a const NC_Copy, while the
copy constructor requires a non-const NC_Copy.

>   It works so on my compiler anyhow.

Strangely, on my compiler (Microsoft Visual C++ 5.0), your example does
what you said: the call to f() is illegal. But my example has no problem.
This is not what I would have predicted.

But in any case, what any particular compiler does has no bearing on
the standard.

> Is it wrong with standard C++?

I don't know. Anyone?

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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: Jim McKelvey <mckelvey@bean.jpl.nasa.gov>
Date: 1998/08/01
Raw View
I notice that math routines are sometimes prototyped in C headers like:

const double cos(const double);
^^^^^

Does this have any special meaning in C++?
---
[ 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: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/08/02
Raw View
Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:

>I notice that math routines are sometimes prototyped in C headers like:
>
>const double cos(const double);
>^^^^^
>
>Does this have any special meaning in C++?

Not in standard C++.

In old versions of GNU C and C++, it meant that the function had no
side effects.  However, this non-standard semantics for `const' on
function return values is not allowed by the ANSI/ISO C standard.
So newer versions of GNU C/C++ use the syntax

 double cos(double) __attribute__((const));

Why do they still use the non-standard syntax `__attribute__' rather
than a #pragma?  I think the main reason for that is that in the current
C standard (C89), #pragmas may not appear in macros.

However, the forthcoming C9X standard extends C89 with a new form of
pragma declaration that *can* be used in macros.  When GNU C
is extended to support C9X they might perhaps allow `pragma attribute(...)'
or something like that as an alternative to `__attribute__((...))'.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.
---
[ 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: haberg@REMOVE.matematik.su.se (Hans Aberg)
Date: 1998/08/02
Raw View
In article <6q1itu$o90$1@mulga.cs.mu.OZ.AU>, fjh@cs.mu.OZ.AU (Fergus
Henderson) wrote:
>Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:
>>const double cos(const double);
>>^^^^^
>>
>>Does this have any special meaning in C++?
>
>Not in standard C++.

  My impression was that this would mean that the returned value cannot
change. For example:
    const char* hello = "Hello";
    const char* name() { return hello; }
Then the next user will know that this is a value that cannot be changed.

  For doubles one might have:
    const double pi = 2 * asin(1.0);
    const double Pi() { return pi; }
Then, with
    void f(double&);
    void g(const double&);
one gets
    main() {
        f(Pi());   // Error: Cannot convert const double -> double&
        g(Pi());   // OK: Can convert const double -> const double&

  It works so on my compiler anyhow. Is it wrong with standard C++?

  Hans Aberg   * Anti-spam: Remove "REMOVE." from email address.
               * Email: Hans Aberg <mailto:haberg@member.ams.org>
               * Home Page: <http://www.matematik.su.se/~haberg/>
               * AMS member listing: <http://www.ams.org/cml/>
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/08/03
Raw View
Hans Aberg wrote:
>
> In article <6q1itu$o90$1@mulga.cs.mu.OZ.AU>, fjh@cs.mu.OZ.AU (Fergus
> Henderson) wrote:
> >Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:
> >>const double cos(const double);
> >>^^^^^
> >>
> >>Does this have any special meaning in C++?
> >
> >Not in standard C++.
>
>   My impression was that this would mean that the returned value cannot
> change. For example:
>     const char* hello = "Hello";
>     const char* name() { return hello; }
> Then the next user will know that this is a value that cannot be changed.

This is not the same, since here not the return type is const, but
what the returned pointer points to. Indeed, the function returns
a non-const pointer to constant char. The analog to const double
would then be

const char* const name2() { return hello; }

which makes no difference to name() (see below).

>
>   For doubles one might have:
>     const double pi = 2 * asin(1.0);
>     const double Pi() { return pi; }
> Then, with
>     void f(double&);
>     void g(const double&);
> one gets
>     main() {
>         f(Pi());   // Error: Cannot convert const double -> double&
>         g(Pi());   // OK: Can convert const double -> const double&
>
>   It works so on my compiler anyhow. Is it wrong with standard C++?

It's correct (except that the last one is no conversion, but a mere
binding of a const double to a reference to const double).

However:

double Pi2() { return pi; }

void test()
{
  f(Pi2()); // Error: Binding rvalue to non-const reference
  g(Pi2()); // Ok: Binding rvalue to const reference is allowed
            // and double -> double const is allowed conversion
}

So for doubles it doesn't make a difference (except the error
message is different ;-)). (Your compiler may say "temporary"
instead of "rvalue"). Generally, this is true for any built-in
type (where pointers to classes are built-in).

However, it *can* make a difference if the return value is not a
built-in type, but a class:

class MyDouble
{
  double d;
public:
  MyDouble(double);
  MyDouble& operator=(MyDouble const&);
  // ...
};

MyDouble f();
MyDouble const g();

int main()
{
  f()=3.14; // Allowed, since it is a member function call!
  g()=1.41; // Error: No operator=(MyDouble const&) const.
// BUT:
  MyDouble& x=f(); // Error: Binding rvalue to non-const reference.
}
---
[ 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: AllanW@my-dejanews.com
Date: 1998/08/03
Raw View
In article <35C1FF44.5ACD@bean.jpl.nasa.gov>,
  mckelvey@bean.jpl.nasa.gov wrote:
> I notice that math routines are sometimes prototyped in C headers like:
>
> const double cos(const double);
> ^^^^^

I'm curious: what compiler does this?

> Does this have any special meaning in C++?

No; the value isn't an lvalue, so it can't be modified anyway. It doesn't
have any special meaning in C, either, last time I looked (a few years
now -- please correct me if it's changed).

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum
---
[ 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: haberg@REMOVE.matematik.su.se (Hans Aberg)
Date: 1998/08/04
Raw View
In article <35C58408.F96D2516@physik.tu-muenchen.de>, Christopher Eltschka
<celtschk@physik.tu-muenchen.de> wrote:
>>         g(Pi());   // OK: Can convert const double -> const double&

< [This] one is no conversion, but a mere
>binding of a const double to a reference to const double).

  I think that the view in C++ might be that it is a type conversion which
consists of a binding to a reference.

  Hans Aberg   * Anti-spam: Remove "REMOVE." from email address.
               * Email: Hans Aberg <mailto:haberg@member.ams.org>
               * Home Page: <http://www.matematik.su.se/~haberg/>
               * AMS member listing: <http://www.ams.org/cml/>
---
[ 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: Gabriel Dos Reis <dosreis@DPTMaths.ens-cachan.fr>
Date: 1998/08/04
Raw View
>>>>> =ABHans=BB, Hans Aberg <haberg@REMOVE.matematik.su.se> wrote:

Hans> In article <6q1itu$o90$1@mulga.cs.mu.OZ.AU>, fjh@cs.mu.OZ.AU (Fergu=
s
Hans> Henderson) wrote:
>> Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:
>>> const double cos(const double);
>>> ^^^^^
>>>=20
>>> Does this have any special meaning in C++?
>>=20
>> Not in standard C++.

Hans>   My impression was that this would mean that the returned value ca=
nnot
Hans> change. For example:
Hans>     const char* hello =3D "Hello";
Hans>     const char* name() { return hello; }
Hans> Then the next user will know that this is a value that cannot be ch=
anged.


You're mixing many (unrelated) issues.


Hans>   For doubles one might have:
Hans>     const double pi =3D 2 * asin(1.0);
Hans>     const double Pi() { return pi; }

For builtin types, it doesn't make any difference. For user defined
types (ie. classes) it does.

 double AnotherPi() { return pi; } // return a builtin type *value*

Hans> Then, with
Hans>     void f(double&);
Hans>     void g(const double&);
Hans> one gets
Hans>     main() {
Hans>         f(Pi());   // Error: Cannot convert const double -> double&
Hans>         g(Pi());   // OK: Can convert const double -> const double&

       g(AnotherPi()); // must succeed: bind const reference
    // to temporary value

--
Gabriel Dos Reis                   |  Centre de Math=E9matiques et de=20
dosreis@cmla.ens-cachan.fr         |         Leurs Applications
Fax : (33) 01 47 40 21 69          |   ENS de Cachan -- CNRS (URA 1611)
               61, Avenue du Pdt Wilson, 94235 Cachan - Cedex
---
[ 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              ]