Skip to content

Compilation error when using temporary as function argument #32

@buech

Description

@buech

The following example fails to compile using the current master or develop branch, tested with gcc-10.3.0 and clang-15.0.4

#include <iostream>

#include <codi.hpp>

template<class T>
T compute(const T &x)
{
  return x / 2.0;
}

using Real = codi::RealReverse;
using Tape = typename Real::Tape;

int main(int nargs, char** args) {
  Real x = 1.0;

  Tape& tape = Real::getTape();
  tape.setActive();

  tape.registerInput(x);
  Real y = compute(x + 1.0);
  tape.registerOutput(y);

  tape.setPassive();
  y.setGradient(1.0);
  tape.evaluate();

  std::cout << "    f(" << x << ") = " << y << std::endl;
  std::cout << "df/dx(" << x << ") = " << x.getGradient() << std::endl;

  tape.reset();

  return 0;
}

with the following error message (gcc):

test.cpp: In instantiation of ‘T compute(const T&) [with T = codi::BinaryExpression<double, codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >, codi::ConstantExpression<double, codi::ConstantDataConversion>, codi::OperationAdd>]’:
test.cpp:21:27:   required from here
test.cpp:8:12: error: could not convert ‘codi::operator/<double, codi::BinaryExpression<double, codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >, codi::ConstantExpression<double, codi::ConstantDataConversion>, codi::OperationAdd> >((*(const codi::ExpressionInterface<double, codi::BinaryExpression<double, codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >, codi::ConstantExpression<double, codi::ConstantDataConversion>, codi::OperationAdd> >*)(& x)), 2.0e+0)’ from ‘BinaryExpression<[...],codi::BinaryExpression<double, codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >, codi::ConstantExpression<double, codi::ConstantDataConversion>, codi::OperationAdd>,[...],codi::OperationDivide>’ to ‘BinaryExpression<[...],codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >,[...],codi::OperationAdd>’
    8 |   return x / 2.0;
      |          ~~^~~~~
      |            |
      |            BinaryExpression<[...],codi::BinaryExpression<double, codi::ActiveType<codi::JacobianLinearTape<codi::JacobianTapeTypes<double, double, codi::LinearIndexManager<int>, codi::DefaultChunkedData, codi::LocalAdjoints> > >, codi::ConstantExpression<double, codi::ConstantDataConversion>, codi::OperationAdd>,[...],codi::OperationDivide>

Returning just x in the function or giving just x as the argument makes it compile. Using const auto x2 = x + 1.0; also gives the error and only const Real x2 = x + 1.0; leads to compiling code and the expected result.

Is that expected behaviour or could it be a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions