相关疑难解决方法(0)

T &&(双&符号)在C++ 11中意味着什么?

我一直在研究C++ 11的一些新功能,我注意到的是在声明变量时使用的双符号,例如T&& var.

首先,这只野兽叫什么?我希望谷歌允许我们搜索这样的标点符号.

究竟是什么意思?

乍一看,它似乎是一个双重参考(如C风格的双指针T** var),但我很难想到一个用例.

c++ c++-faq rvalue-reference perfect-forwarding c++11

747
推荐指数
4
解决办法
18万
查看次数

为函数返回的右值引用赋值

#include <utility>
template <typename Container>
decltype(auto) index(Container &&arr, int n) {
    return std::forward<Container>(arr)[n];
}
Run Code Online (Sandbox Code Playgroud)

进行函数调用:

#include <vector>
index(std::vector {1, 2, 3, 4, 5}, 2) = 0;
Run Code Online (Sandbox Code Playgroud)

当函数调用完成时,对象std::vector {1, 2, 3, 4, 5}将被销毁,为释放的地址赋值会导致未定义的行为。但是上面的代码运行良好,valgrind 什么也没检测到。也许编译可以帮助我制作另一个不可见的变量,例如

auto &&invisible_value {index(std::vector {1, 2, 3, 4, 5}, 2)};
invisible_value = 9;
Run Code Online (Sandbox Code Playgroud)

如果我的猜测不正确,我想知道为什么为从函数返回的右值引用赋值是可行的,以及临时对象index(std::vector {1, 2, 3, 4, 5}, 2)何时会被销毁。

这个想法起源于?Effective Modern C++?,Item3:理解decltype

c++ rvalue rvalue-reference c++11 c++14

9
推荐指数
1
解决办法
331
查看次数

错误:使用标签时使用&&运算符时未定义

int main()
{
        int i = 0;
        int *p = &i;
        int *q = &&i;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用gccon 编译时Linux,我收到错误

addr.c: In function ‘main’:
addr.c:6:2: error: label ‘i’ used but not defined
Run Code Online (Sandbox Code Playgroud)

为什么编译器处理int ilabel,而不是整数?我们什么时候使用&& operator

编辑:好的,我可以在一定程度上理解答案,但是你可以从"arch/arm/include/asm/processor.h"解释下面的宏定义.它没有说什么label,但评论说,它可以返回" program counter"

/*
 * Default implementation of macro that returns current
 * instruction pointer ("program counter").
 */
#define current_text_addr() ({ __label__ _l; _l: &&_l;})
Run Code Online (Sandbox Code Playgroud)

c c++ linux gcc operator-overloading

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

在&bitlo_cast <void*>(&& __ opcode)的上下文中&&的含义;

我正在查看下面的代码来自JavascriptCore,我不知道&&的含义在下面的上下文中.地址的地址实际上没有意义.

那么有人可以在下面的上下文中解释&&的含义.

(bitwise_cast使用联合来避免reinterpret_cast带来的严格别名问题)

下面的代码编译clang(可能是gcc),但不能在我们自己的专有C++编译器上编译.

完整的来源可以在这里找到.

#if ENABLE(COMPUTED_GOTO_OPCODES)
    Opcode* opcodeMap = LLInt::opcodeMap();
    #define OPCODE_ENTRY(__opcode, length) \
    opcodeMap[__opcode] = bitwise_cast<void*>(&&__opcode); //<---- The double && 
    FOR_EACH_OPCODE_ID(OPCODE_ENTRY)
    #undef OPCODE_ENTRY

    #define LLINT_OPCODE_ENTRY(__opcode, length) \
        opcodeMap[__opcode] = bitwise_cast<void*>(&&__opcode);

    FOR_EACH_LLINT_NATIVE_HELPER(LLINT_OPCODE_ENTRY)
    #undef LLINT_OPCODE_ENTRY
#endif
Run Code Online (Sandbox Code Playgroud)

c++ webkit

4
推荐指数
1
解决办法
193
查看次数