Universal link with class template

Example:

template <typename T>
class Bar
{
public:
    void foo(T&& arg)
    {
        std::forward<T>(arg);
    }
};

Bar<int> bar;    

bar.foo(10); // works

int a{ 10 };
bar.foo(a); // error C2664: cannot convert argument 1 from 'int' to 'int &&'

Universal links seem to work only with function templates and only with type subtraction, right? So it makes no sense to use it with a class? And does use really std::forwardmake sense in my case?

+4
source share
2 answers

Note that the preferred terminology (i.e., that which will be in future versions of the specification) now forwards the link.

, . , T&&, T - int. int&, Bar. , , .

-, -:

template <typename U>
void foo(U&& arg)
{
    std::forward<U>(arg); //actually do something here
}

, U , T, static_assert:

template <typename U>
void foo(U&& arg)
{
    static_assert(std::is_same<std::decay_t<U>,std::decay_t<T>>::value, 
                  "U must be the same as T");
    std::forward<U>(arg); //actually do something here
}

std::decay , . , , :

template <typename T>
using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;

template <typename T, typename U>
using is_equiv = std::is_same<remove_cv_ref<T>, remove_cv_ref<U>>;

, are_equiv. , , . bool_pack:

namespace detail
{
    template<bool...> struct bool_pack;
    template<bool... bs>
    using all_true = std::is_same<bool_pack<bs..., true>, bool_pack<true, bs...>>;
}
template <typename... Ts>
using all_true = detail::all_true<Ts::value...>;

- , Ts... Us... is_equiv. , std:: tuple ( node , ):

template <typename TTuple, typename UTuple>
struct are_equiv;

template <typename... Ts, typename... Us>
struct are_equiv <std::tuple<Ts...>, std::tuple<Us...>> : all_true<is_equiv<Ts,Us>...>
{};

:

static_assert(are_equiv<std::tuple<Ts...>,std::tuple<Us...>>::value, 
              "Us must be equivalent to Ts");
+5

: " " , T&&. (T ), , .

std::forward std::move, arg rvalue.

, foo :

template <typename T>
class Bar
{
public:
    template <typename U>
    void foo(U&& arg)
    {
        std::forward<U>(arg);
    }
};
+2

All Articles