Skip to content

Compilation error when using temporary as function argument to a template function #33

Answered by MaxSagebaum
buech asked this question in Q&A
Discussion options

You must be logged in to vote

Short answer: It is expected behavior.

Long answer: CoDiPack uses expression templates to reduce the memory footprint of the recorded tapes. Therefore, we return the expressions and not the computed value. Only an assignment converts the expression to a value.

The automatic deduction of the template parameter for compute therefore deduces the expression type. C++ does not give us the means to change the deduction behavior.

There are two ways to fix this problem:

  • On the declaration side C++14: (The nice.)
template<class T>
auto compute(const T &x) 
{
  return x / 2.0;
}
  • On the declaration side C++11: (The ugly.)
template<class T>
auto compute(const T &x)  -> decltype(x / 2.0)
{
  retu…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by MaxSagebaum
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #32 on August 15, 2023 07:36.