c++ - 创建引用的类模板包装器会导致未定义的行为吗?

标签 c++ templates reference wrapper undefined-behavior

这是一个代码片段:

#include <iostream>
#include <string>
#include <vector>

template<class T>
class Wrapper {
public:
    T& t;
    explicit Wrapper2( T& obj ) : t(obj) {} 
};

class Car {
public:
    std::string color;
    std::string name;
    Car(){}
    Car( std::string colorIn, std::string nameIn ) : color( colorIn ), name( nameIn ){}
};

int _tmain( int iNumArguments, _TCHAR* pArgumentText[] ) {
    typedef Wrapper<Car> car;

    // Create 2 Containers
    std::vector<car> collection1;
    std::vector<car> collection2;

    // Populate collection 1 with reference objects
    collection1.push_back( Car("black", "Ford") );
    collection1.push_back( Car("white", "BMW") );
    collection1.push_back( Car("yellow", "Audi") );

    // use referenced object at index 0 in collection 1
    // to populate index 0 of collection 2
    collection2.push_back( collection1[0] );

    // Print Out index 0 of collection2's referenced object's fields
    std::cout << collection2[0].t.color << " " << collection2[0].t.name << std::endl;

    // Change collection2's index 0 referenced object's fields
    collection2[0].t.color = std::string( "green" );
    collection2[0].t.name  = std::string( "Gremlin" );

    // Print out collection1's index 0 referenced object's fields
    // and they are changed
    std::cout << collection1[0].ptr->color << " " << collection1[0].ptr->name << std::endl;
    return 0;
}

这确实在 MSVS2015 CE 中成功编译、构建和运行,没有任何错误。

这是否会产生未定义的行为,如果是的话;怎么办?

最佳答案

Will creating a class template wrapper of a reference cause undefined behavoir?

没有。

然而,滥用引用(以及指针和迭代器)通常会造成这种情况。如果引用的对象被销毁,则该引用将悬空。这发生在所有引用上,而不仅仅是那些包装在类中的引用——包装器没有效果。当引用悬挂时,使用它具有未定义的行为。

collection1.push_back( Car("black", "Ford") );

在这里,对象是临时的。该对象一直存在到 push_back 结束。之后, vector 中包装器中的引用是悬空的。

std::cout << collection2[0].t.color << " " << collection2[0].t.name << std::endl;

这里你使用了悬空引用,程序有未定义的行为。


这是使用没有 UB 的包装器的示例:

Car c;
collection1.push_back(c);
collection1[0].color = "black";

关于c++ - 创建引用的类模板包装器会导致未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41483552/

相关文章:

go - 当我在 Goroutine 中填充它时,为什么这张 map 是空的?

asp.net - 在 ASP 中使用其他项目的用户控件

c++ - C/C++ 中是否有与 GNU-R which() 等效的函数?

Django:模板中的动态 URL 别名

asp.net - 动态更改 GridView 项模板

c++ - static_assert 如何用于检查模板函数的迭代器参数的元素类型?

r - 准确理解何时 data.table 是对另一个 data.table 的引用(而不是其副本)

c++ - react native JSI : How to call any javascript function from native?

c++ - 遍历数组并找到最接近键的数组元素的最佳方法是什么?

java - 列的算法,来自索引的行?