相关疑难解决方法(0)

什么是"这个的右值参考"?

在clang的C++ 11状态页面中遇到了一个名为"rvalue reference for*this"的提案.

我已经阅读了很多关于rvalue引用并理解它们的内容,但我认为我不知道这一点.我也无法使用这些条款在网上找到太多资源.

页面上的提案文件有一个链接:N2439(将移动语义扩展到*this),但我也没有从中获得太多的例子.

这个功能是什么?

c++ qualifiers c++-faq move-semantics c++11

229
推荐指数
3
解决办法
2万
查看次数

为什么在重载方法上使用引用限定符存在歧义?

以下代码在Clang中编译,但在GCC中不编译.当我operator()使用左值调用时,它可以工作,但不能使用右值.为什么?

struct S
{
    bool operator()() const &  { return true; }
    bool operator()() const && { return true; }
};

int main()
{
    S s;

    s();   // works
    S()(); // fails (error: call of '(S) ()' is ambiguous)
}
Run Code Online (Sandbox Code Playgroud)

我正在GCC 4.8上编译这段代码.

c++ c++11

10
推荐指数
0
解决办法
101
查看次数

具有 xvalue 操作数的三元运算符

假设我们有一个带有 2 个 xvalue 操作数的三元运算符。

struct A {
    A() { std::cout<<"A ctor"<<std::endl; }
    ~A() { std::cout<<"A dtor"<<std::endl; }
    A(A const&) { std::cout<<"A copy ctor"<<std::endl; }
    A(A&&) { std::cout<<"A move ctor"<<std::endl; }

    void foo() & { std::cout<<"A&.foo()"<<std::endl; }
    void foo() const& { std::cout<<"A const&.foo()"<<std::endl; }
    void foo() && { std::cout<<"A&&.foo()"<<std::endl; }
    void foo() const&& { std::cout<<"A const&&.foo()"<<std::endl; }
};

int main()
{
    A a;
    A a2;
    (true? static_cast<A&&>(a) : static_cast<A&&>(a2)).foo();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据cppreference条件运算符

4) 如果 E2 和 E3 是相同类型和相同值类别的左值,则结果具有相同类型和值类别,并且如果 E2 …

c++ c++11 c++14 c++17 value-categories

7
推荐指数
1
解决办法
220
查看次数