Topic: Pass by value for f(const T&)
Author: Marco Dalla Gasperina <marcodg@pacifier.com>
Date: 1998/10/27 Raw View
Is a compiler allowed to pass a variable by value
when calling a function declared:
void f(const T&);
Clearly this invalidates the use of const_cast<>
inside the body of f (but that evokes undefined
behavior anyway). Still, this could be an optimization
when T is a basic type.
In fact, it looks like it could only be valid for
a basic type since it would involve a copy ctor
otherwise. True?
thanks,
marco
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/10/27 Raw View
On 27 Oct 1998 17:17:24 GMT, Marco Dalla Gasperina <marcodg@pacifier.com> wrote:
>Is a compiler allowed to pass a variable by value
>when calling a function declared:
>
>void f(const T&);
template <typename T> void f(const T& a, T& b)
{
cout << a << '\n';
b++;
cout << a << '\n';
}
int main()
{
int i=3;
f(i,i); // must pass both args by reference!
}
>Clearly this invalidates the use of const_cast<>
>inside the body of f (but that evokes undefined
>behavior anyway). Still, this could be an optimization
>when T is a basic type.
If the function f(...) is inline, and it is indeed inlined, it
doesn't matter.
>In fact, it looks like it could only be valid for
>a basic type since it would involve a copy ctor
>otherwise. True?
Basic types must be copied too! Anyway, as long as the optimization
doesn't change the observable behaviour of a program, it is valid.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]