为何c-的const_cast自动类型推断失效,长尾如何表述?

2026-04-16 18:422阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计415个文字,预计阅读时间需要2分钟。

为何c-的const_cast自动类型推断失效,长尾如何表述?

在我工作中,const_cast的运用在某些情况下是不可避免的。目前我必须使用const_cast处理一些非常复杂的类型,实际上我并不想在Clutter中混入所有这类类型。

在我的工作中,const_cast的使用在某些情况下是不可避免的.

现在我必须const_cast一些非常复杂的类型,实际上我不想在const_cast< Clutter>中写出所有这种类型的混乱.表达式,特别是如果Clutter非常长.

我的第一个想法是编写const_cast<>(myType),但是我的编译器无法推导出myType的非const类型.所以我想帮助我的编译器和我编写以下方法,编译.

#include <stdlib.h> #include <iostream> int main(int, char**) { const int constVar = 6; using T = typename std::remove_cv<decltype(constVar)>::type; auto& var = const_cast<T&>(constVar); var *= 2; std::cout << &constVar << " " << &var << "\n"; // Same address! std::cout << constVar << " " << var << "\n"; return EXIT_SUCCESS; }

不幸的是,程序给了我输出6 12而不是预期的6 6,我真的不明白?

我的做法有什么问题?

const_cast的文档:

const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

所以你拥有的是未定义的行为.

同样令人感兴趣的是cv type qualifiers的这张纸条.

const object – an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.

为何c-的const_cast自动类型推断失效,长尾如何表述?

本文共计415个文字,预计阅读时间需要2分钟。

为何c-的const_cast自动类型推断失效,长尾如何表述?

在我工作中,const_cast的运用在某些情况下是不可避免的。目前我必须使用const_cast处理一些非常复杂的类型,实际上我并不想在Clutter中混入所有这类类型。

在我的工作中,const_cast的使用在某些情况下是不可避免的.

现在我必须const_cast一些非常复杂的类型,实际上我不想在const_cast< Clutter>中写出所有这种类型的混乱.表达式,特别是如果Clutter非常长.

我的第一个想法是编写const_cast<>(myType),但是我的编译器无法推导出myType的非const类型.所以我想帮助我的编译器和我编写以下方法,编译.

#include <stdlib.h> #include <iostream> int main(int, char**) { const int constVar = 6; using T = typename std::remove_cv<decltype(constVar)>::type; auto& var = const_cast<T&>(constVar); var *= 2; std::cout << &constVar << " " << &var << "\n"; // Same address! std::cout << constVar << " " << var << "\n"; return EXIT_SUCCESS; }

不幸的是,程序给了我输出6 12而不是预期的6 6,我真的不明白?

我的做法有什么问题?

const_cast的文档:

const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

所以你拥有的是未定义的行为.

同样令人感兴趣的是cv type qualifiers的这张纸条.

const object – an object whose type is const-qualified, or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.

为何c-的const_cast自动类型推断失效,长尾如何表述?