C++ 为什么编译右值上运算符的调用地址?

C++ 为什么编译右值上运算符的调用地址?,c++,iterator,addressof,C++,Iterator,Addressof,AFAIK获取右值的地址是非法的,因为操作符&的地址获取左值并返回对象的调用地址的地址 下面是一个我试图理解但有点困惑的例子: #include <deque> #include <iostream> int main() { using std::cout; using std::endl; std::deque<int> di{ 1, 1, 2, 3, 5, 8, 13 }; std::deque<int>

AFAIK获取右值的地址是非法的,因为操作符
&
的地址获取左值并返回对象的调用地址的地址

下面是一个我试图理解但有点困惑的例子:

#include <deque>
#include <iostream>

int main() {
    using std::cout;
    using std::endl;

    std::deque<int> di{ 1, 1, 2, 3, 5, 8, 13 };

    std::deque<int>::iterator it = di.end() - 1; 

    cout << *it << endl;
    cout << &it-- << endl; // is it UB?
    cout << *it << endl;
    cout << &it-- << endl;
    cout << *it << endl;
    cout << &it-- << endl;
    cout << *it << endl << endl;

    cout << &--it << endl; // ok
    cout << *it << endl; // ok
    cout << &--it << endl; // ok
    cout << *it << endl; // ok



    std::cout << std::endl << "done!" << std::endl;
}
同样在MSVC++14上使用
/Wall

  • 那么在我的代码中它是未定义的行为吗

  • <>为什么C++允许在RUVE上调用操作符<代码>和<代码>的地址,而标准不允许它?< /LI>
    最后,我想知道在这样的表达式中,不同的地址来自哪里:<代码> STD::CUT为什么C++允许操作员调用地址,而在标准不允许的情况下,R值是?”-我不明白这一点——C++和标准没有区别,它是不允许的。你的意思是问编译器为什么允许这样做吗?@StoryTeller:是的,我是认真的。这不是未定义的行为:定义的行为是编译器错误,因为这在标准中是不允许的。您可能需要深入研究生成的程序集,以查看编译器在这种情况下的操作正确的术语是“格式错误”。@arithma:您的意思是“未定义的行为是编译器错误…”?