I have a doubt about if that approach is possible, lets say you want two ways of calling a function at same time, one is returning an object and the other return by reference in parameter:
// ...
template <class T> void func(Foo<T>& f, const T n)
{
f.a = Something(f.a + n);
f.b = Something(f.b + n);
}
template <class T> Foo<T> func(const Foo<T>& f, const T n)
{
return Foo<T>( Something(f.a + n), Something(f.b + n) );
}
// ...
// main
Foo<int> foo(1, 1);
func(foo, 2);
Foo<int> foo2 = func(foo, 2);
The const word in first parameter affect the signature of method?
Yes, a reference to
constis a separate type to a reference to non-const, so the two functions with these arguments are separate overloads.However, this will not work:
Since
foois notconst, this will select the non-constoverload, which has no return value; so the assignment will fail. You would need to explicitly choose theconstversion: