Re: rvalues and lvalues
Am 03.11.2011 19:15, schrieb Jerry:
How do I write a function that will accept both rvalues and lvalues?
For example, if I write this (contrived example):
#include<iostream>
#include<vector>
std::vector<int> & operator<< (std::vector<int> & x, const
std::vector<int>::value_type& y)
{
x.push_back(y);
return x;
}
int main()
{
std::cout<< (std::vector<int>()<< 1)[0]<< std::endl;
}
Then I get:
Dynamo.cpp: In function 'int main()':
Dynamo.cpp:12:41: error: no match for 'operator<<' in
'std::vector<int>()<< 1'
Dynamo.cpp:12:41: note: candidates are:
Dynamo.cpp:4:20: note: std::vector<int>& operator<<(std::vector<int>&,
const value_type&)
Dynamo.cpp:4:20: note: no known conversion for argument 1 from
'std::vector<int>' to 'std::vector<int>&'
But If I change the code to:
#include<iostream>
#include<vector>
std::vector<int> operator<< (std::vector<int> x, const
std::vector<int>::value_type& y)
{
x.push_back(y);
return x;
}
int main()
{
std::vector<int> a;
a<< 1;
std::cout<< a[0]<< std::endl;
}
Then I get a segmentation fault because the vector was copied and the
change was made to the copy so the original has no 0th element.
How do I write such a thing that will accept both lvalues and rvalues?
Either add an overload that takes an rvalue-reference, like so:
std::vector<int> & operator<<(std::vector<int> && x,
const std::vector<int>::value_type & y)
{
x.push_back(y);
return x;
}
or replace the function by a (possibly constrained) function template that uses perfect forwarding:
[your includes]
#include <type_traits>
template<class T>
struct is_vector_impl : std::false_type {};
template<class T, class A>
struct is_vector_impl<std::vector<T, A>> : std::true_type {};
template<class T>
struct is_vector : is_vector_impl<typename std::remove_reference<T>::type>
{
};
template<class T,
class = typename std::enable_if<is_vector<T>::value>::type
std::vector<int> & operator<<(T&& x,
const std::vector<int>::value_type & y)
{
x.push_back(y);
return x;
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]