Topic: reference type and template parameter
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: Tue, 9 Oct 2001 01:11:19 GMT Raw View
tinct@163.com (Tinct) writes:
> When we use a reference type as template parameter, it will be treat
> as type not reference type. Should one must explicit specialize the
> template?
Toplevel references easily disappear in a number of places in C++
(causing the values just to be lvalues of their type). So if you want
a Sum class, you best do
class Sum {
int &s;
public:
Sum(int &p):s(p){}
Sum(const Sum& o):s(o.s){}
void operator()(int i){ s+= i;};
//...
}
for_each(v.begin(),v.end(),Sum(my_sum));
where my_sum is an int.
Regards,
Martin
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: tom_usenet@hotmail.com (Tom)
Date: Tue, 9 Oct 2001 17:56:42 GMT Raw View
On Mon, 8 Oct 2001 20:16:47 GMT, tinct@163.com (Tinct) wrote:
>1. Some results: ( Under VC6/BCC5 )
>int i;
>std::cout << typeid(i).name(); // int
>std::cout << typeid((int &)i).name(); // int (??)
>std::cout << typeid(int &).name(); // int &
The standard says that:
typeid(int) == typeid(const int&)
Both top level cv qualification and reference bits are ignored.
>
>2. Another result:
>class Sum {
> int s;
>public:
> void operator()(int i){ s+= i;};
>//...
>}
>
>...
>Sum my_sum;
>for_each(v.begin(),v.end(),my_sum); // my_sum not changed!
>for_each(v.begin(),v.end(),(Sum &)my_sum); // my_sum not changed!
>(??)
>for_each<vector<int>::iterator,Sum &>(v.begin(),v.end(),my_sum);
>// now my_sum.s is sum of all element in v
How about:
my_sum = for_each(v.begin(),v.end(), my_sum);
my_sum is both passed and returned by value here, since that's how
for_each is written. You can force it to pass by reference though, as
in your last example. Not recommended though, since for_each isn't
guaranteed not to go off and make a copy of the passed parameter
anyway, just for the hell of it.
>
>3. Question:
>When we use a reference type as template parameter, it will be treat
>as type not reference type. Should one must explicit specialize the
>template?
Function templates deduce their argument types in such a way that for
a parameter type T, if it is passed a reference type it will still
deduce T as the non-reference type. This is a good rule, since it
allows you to write:
template <class T>
void func1(T t);
and
template <class T>
void func2(T& t);
to do different things. for_each however takes its last argument like
func1.
Tom
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 9 Oct 2001 18:03:37 GMT Raw View
Tinct wrote:
>
> 1. Some results: ( Under VC6/BCC5 )
> int i;
> std::cout << typeid(i).name(); // int
> std::cout << typeid((int &)i).name(); // int (??)
> std::cout << typeid(int &).name(); // int &
The string returned by std::typeinfo::name() is implementation-defined.
It can be literally anything, including being exactly the same string
for every type.
> 2. Another result:
> class Sum {
> int s;
> public:
> void operator()(int i){ s+= i;};
> //...
> }
>
> ...
> Sum my_sum;
> for_each(v.begin(),v.end(),my_sum); // my_sum not changed!
> for_each(v.begin(),v.end(),(Sum &)my_sum); // my_sum not changed!
> (??)
> for_each<vector<int>::iterator,Sum &>(v.begin(),v.end(),my_sum);
> // now my_sum.s is sum of all element in v
for_each() makes a copy of it's third argument, applies that copy to
each element of the iterator range, and then returns that copy. The
original never gets touched. Therefore, what you're supposed to do is:
my_sum = for_each(v.begin(), v.end(), my_sum);
However, given the 'copy' semantics for references, the approach where
you explicitly make the 'Function' type be 'Sum &' will also work. So,
why must you specify it explicitly? Why isn't 'Sum &' deduced in your
second case? It's because of 14.8.2.1p2: "... If P is a reference type,
the type referred to by P is used for type deduction."
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: tinct@163.com (Tinct)
Date: Mon, 8 Oct 2001 20:16:47 GMT Raw View
1. Some results: ( Under VC6/BCC5 )
int i;
std::cout << typeid(i).name(); // int
std::cout << typeid((int &)i).name(); // int (??)
std::cout << typeid(int &).name(); // int &
2. Another result:
class Sum {
int s;
public:
void operator()(int i){ s+= i;};
//...
}
...
Sum my_sum;
for_each(v.begin(),v.end(),my_sum); // my_sum not changed!
for_each(v.begin(),v.end(),(Sum &)my_sum); // my_sum not changed!
(??)
for_each<vector<int>::iterator,Sum &>(v.begin(),v.end(),my_sum);
// now my_sum.s is sum of all element in v
3. Question:
When we use a reference type as template parameter, it will be treat
as type not reference type. Should one must explicit specialize the
template?
---
[ 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://www.research.att.com/~austern/csc/faq.html ]