小编Ser*_*gei的帖子

reinterpret_cast会导致未定义的行为吗?

我有一个类模板A,其中包含一个指针容器(T*):

template <typename T>
class A {
public:
   // ... 
private:
   std::vector<T*> data;
};
Run Code Online (Sandbox Code Playgroud)

和一堆功能,如:

void f(const A<const T>&);
void g(const A<const T>&);
Run Code Online (Sandbox Code Playgroud)

是否可以将呼叫通过铸造这些功能从A<const T>A<T>

A<double> a;
... 
auto& ac = reinterpret_cast<const A<const double>&>(a);
f(ac);
Run Code Online (Sandbox Code Playgroud)

我很确定这段代码有不确定的行为.

在现实生活中使用这种转换是危险的吗?

c++ templates reinterpret-cast

8
推荐指数
2
解决办法
1876
查看次数

标签 统计

c++ ×1

reinterpret-cast ×1

templates ×1