Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return modified value from a function template error

Tags:

c++

templates

My code is something like this:

   // ... code

   template <int i>
   int modifyparameter()
   {
     i = i++;
     return i;
   }

   // ... some more code

   int main()
   {
        int answer = modifyparameter<5>();
        cout << answer; //expecting 6
   }

But I am getting errors. What have I done wrong?

like image 338
student Avatar asked Dec 22 '22 22:12

student


1 Answers

i is the name of an int value, and you cannot modify values. You probably want one of these:

template <typename Number>
Number functional(Number x)
{
    return x + 1;
}

template <typename Number>
Number& side_effect(Number& x)
{
    return ++x;
}
like image 199
fredoverflow Avatar answered Jan 06 '23 10:01

fredoverflow