我有一个类模板 A
,其中包含指针容器(T*
):
template <typename T>
class A {
public:
// ...
private:
std::vector<T*> data;
};
还有一堆像这样的函数:
void f(const A<const T>&);
void g(const A<const T>&);
这些函数可以通过将
A<const T>
转换为A<T>
来调用吗?A<double> a;
...
auto& ac = reinterpret_cast<const A<const double>&>(a);
f(ac);
我非常确定这段代码存在未定义的行为。
在现实生活中使用这种转换是否危险?
this == nullptr
荒谬的代码,它们在gcc6中被破坏。 - Baum mit Augen5.2.10 Reinterpret cast
部分:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4582.pdf - Jesper JuhlA<const T>
和A<T>
调用。我不能只保留A<T>
而放弃A<const T>
,否则就会失去 const-correctness。 - Sergei