template<typename T, bool move_constructible = std::is_move_constructible<T>::value>
struct choose_best_input_modifier;
template<typename T>
struct choose_best_input_modifier<T, true>
{
typedef T&& type;
};
template<typename T>
struct choose_best_input_modifier<T, false>
{
typedef const T& type;
};
#define MOVED(...) choose_best_input_modifier<__VA_ARGS__>::type
#define MOVED_T(...) typename choose_best_input_modifier<__VA_ARGS__>::type // the typename issue, just another issue, but ok...
template<typename T>
void func(MOVED_T(T) x) // T&& if available or const T& otherwise
{
cout << typeid(x).name();
}
void func2(MOVED(string) x) // string&&
{
cout << typeid(x).name();
}
void funcNM(MOVED(NM) nm) // const NM&
{
cout << typeid(nm).name();
}
But I can't...
template<class T>
struct is_move_constructible :
std::is_constructible<T, typename std::add_rvalue_reference<T>::type> {};The problem is that in this case it will be true for any class which is copy constructible, which is not the desired result.
For example I would like to implement something like this:
On 2016–09–25, at 5:36 PM, Alexey Mamontov <cara...@gmail.com> wrote:The problem is that in this case it will be true for any class which is copy constructible, which is not the desired result.
On 2016–09–26, at 1:47 PM, Nicol Bolas <jmck...@gmail.com> wrote:That would lead to dysfunctional code. Imagine a class like `std::list`, but is move-only. Such a class would not be nothrow move constructible, and it wouldn't be copy constructible. So... what do you pick?
At the end of the day, there's no way to guaranteeably know if moving is more efficient than copying. However, moving will in virtually every case be no less efficient than copying (even with `std::list`).