小编Dav*_*ve 的帖子

C ++转发参考和r值参考

我知道转发引用是“对cv不合格模板参数的右值引用”,例如

template <class T> void foo(T&& );
Run Code Online (Sandbox Code Playgroud)

这意味着上述函数可以同时使用l值和r值参考。

我有些不懂的事,例如

template <class T>
class A
{
    template <class U>
    void foo(T&& t, U&& u)
    {
        T t2( std::forward(t) ); // or should it be std::move(t)? is T&& forwarding or r-value reference
        U u2( std::forward(u) ); // or should it be std::move(u)? I believe U&& is forwarding reference
    }
};
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,T &&和U &&都是转发参考吗?

我写了一些代码进行测试(VS2015编译器):

class A
{
public:
    A(){};
    A(const A& rhs)
    {
        std::cout << "calling 'const A&' l-value" << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference forwarding-reference

4
推荐指数
2
解决办法
1196
查看次数