Changeset 742


Ignore:
Timestamp:
May 30, 2017 4:53:21 AM (4 months ago)
Author:
kulshres
Message:

Mark cast operators that return constant values as const

This patch changes the operator double const&() for both taped and
tapeless adoubles to be marked as constant functions. This allows
explicit conversion of an adouble to a double in all contexts
(namely both const and non-const class member functions).

Example:
`c++
#include <adolc/adouble.h>
#include <iostream>

struct Printer
{

void print_double_const(const adouble &val) const
{

std::cout << "val (const): " << static_cast<double>(val) <<

std::endl;

}

void print_double(const adouble &val)
{

std::cout << "val (non-const): " << static_cast<double>(val) <<

std::endl;

}

};

int main (int argc, char *argv[])
{

Printer printer;
const adouble val = 1.0;

printer.print_double(val);
printer.print_double_const(val);

}
`

Expected output (achieved with this patch):
`sh
val (non-const): 1
val (const): 1
`

Compiler error (master branch @ 0633b374):
`sh
<path>/test.cc:8:37: error: no matching conversion for static_cast from
'const adouble' to 'double'

std::cout << "val (const): " << static_cast<double>(val) <<

std::endl;

~

<path>/include/adolc/adouble.h:114:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double();


<path>/include/adolc/adouble.h:115:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double const&();


<path>/include/adolc/adouble.h:116:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double&&();


<path>/test.cc:13:41: error: no matching conversion for static_cast from
'const adouble' to 'double'

std::cout << "val (non-const): " << static_cast<double>(val) <<

std::endl;

~

<path>/include/adolc/adouble.h:114:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double();


<path>/include/adolc/adouble.h:115:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double const&();


<path>/include/adolc/adouble.h:116:14: note: candidate function not
viable: 'this' argument has type 'const adouble', but method is not
marked const

explicit operator double&&();

`

Location:
trunk/ADOL-C
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ADOL-C/include/adolc/adouble.h

    r709 r742  
    113113    }
    114114    explicit operator double();
    115     explicit operator double const&();
     115    explicit operator double const&() const;
    116116    explicit operator double&&();
    117117    void setValue ( const double );
  • trunk/ADOL-C/include/adolc/adtl.h

    r708 r742  
    219219    inline double getADValue(const unsigned int p) const;
    220220    inline void setADValue(const unsigned int p, const double v);
    221     inline explicit operator double const&();
     221    inline explicit operator double const&() const;
    222222    inline explicit operator double&&();
    223223    inline explicit operator double();
     
    16551655
    16561656/*******************  getter / setter  **************************************/
    1657 inline adouble::operator double const & () {
     1657inline adouble::operator double const & () const {
    16581658    if (no_do_val()) {
    16591659        fprintf(DIAG_OUT, "ADOL-C error: Tapeless: Incorrect mode, call setMode(enum Mode mode)\n");
  • trunk/ADOL-C/src/adouble.cpp

    r739 r742  
    230230}
    231231
    232 badouble::operator double const&() {
     232badouble::operator double const&() const {
    233233    ADOLC_OPENMP_THREAD_NUMBER;
    234234    ADOLC_OPENMP_GET_THREAD_NUMBER;
Note: See TracChangeset for help on using the changeset viewer.