在 C++ 中默认设置所有类型为常量

发布于 2024-08-31 16:56:14 字数 752 浏览 6 评论 0原文

向编译器指示的最简单、最不唐突的方式是什么,无论是通过编译器选项、#define、typedef 还是模板,每次我说T,我真的是说T const?我不想使用外部预处理器。由于我不使用 mutable 关键字,因此重新调整用途以指示可变状态是可以接受的。

编辑:由于这样做的意图完全是错误的(并且由于我几个小时没有在场澄清),让我解释一下。本质上,我只是想知道哪些系统可用于在编译时操作类型系统。我不在乎这是否会产生非标准的、糟糕的、不可维护的、无用的代码。我不会在生产中使用它。这只是一种好奇心。

到目前为止潜在的(次优)解决方案:

// I presume redefinition of keywords is implementation-defined or illegal.
#define int int const
#define ptr * const
int i(0);
int ptr j(&i);

typedef int const Int;
typedef int const* const Intp;
Int i(0);
Intp j(&i);

template<class T>
struct C { typedef T const type; typedef T const* const ptr; };
C<int>::type i(0);
C<int>::ptr j(&i);

What is the simplest and least obtrusive way to indicate to the compiler, whether by means of compiler options, #defines, typedefs, or templates, that every time I say T, I really mean T const? I would prefer not to make use of an external preprocessor. Since I don't use the mutable keyword, that would be acceptable to repurpose to indicate mutable state.

Edit: Since the intent of this was mistaken entirely (and since I wasn't around for a few hours to clarify), let me explain. In essence, I just want to know what systems are available for manipulating the type system at compile time. I don't care if this creates nonstandard, bad, unmaintainable, useless code. I'm not going to use it in production. It's just a curiosity.

Potential (suboptimal) solutions so far:

// I presume redefinition of keywords is implementation-defined or illegal.
#define int int const
#define ptr * const
int i(0);
int ptr j(&i);

typedef int const Int;
typedef int const* const Intp;
Int i(0);
Intp j(&i);

template<class T>
struct C { typedef T const type; typedef T const* const ptr; };
C<int>::type i(0);
C<int>::ptr j(&i);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

极致的悲 2024-09-07 16:56:14

采用开源 C++ 编译器并对其进行修改。

我认为投反对票的主要原因是人们认为您正在尝试修改 C++。相反,告诉他们您正在创建一种名为“C-const”的新语言作为大学项目。

就我个人而言,我认为这是一个有趣的想法 - 您可以从不可变类型中获得各种性能和可读性收益 - 只要看看大多数函数式语言即可。

Take an open source C++ compiler and modify it.

I think the main reason for the downvotes is that people think you're trying to modify C++. Tell them instead you're creating a new language called "C-const" as a university project.

Personally I think it's an interesting idea - you can gain all sorts of performance and readability gains from immutable types - just look at most functional languages.

以酷 2024-09-07 16:56:14

您是否试图告诉编译器,或者告诉其他人阅读或使用您的代码?编译器不会仅仅因为使用了用户定义类型 const 就做任何不同的事情。实际上,它所做的只是更改可与该对象一起使用的方法集(用户定义的或隐式的)。反过来,可能允许编译器推断出对运行时表示的一些优化。

对于 class/struct 类型,您可以通过简单地将每个成员设为 const 来向编译器和用户明确这一点:

class Point {
    // An immutable Point data object
    public:
        Point(int ix, int iy): x(ix), y(iy) { }
        Point(const Point& p): x(p.x), y(p.y) { }

        Point add(const Point& p) const;
        int taxiDistance() const;
        // etc... all const members

        const int x, y; // const can only be init'd at construction time

     private:
        Point& operator=(const Point& p); // never implemented!
}

Are you trying to tell the compiler, or tell other people reading or using your code? The compiler won't do much anything different just because a user defined type is used const. Really, all it does is change the set of methods (user defined or implicit) that can be used with that object. In turn, that may allow the compiler to infer some optimizations on the run-time representation.

For class/struct types, you can make this clear to both the compiler and users by simply making every member const:

class Point {
    // An immutable Point data object
    public:
        Point(int ix, int iy): x(ix), y(iy) { }
        Point(const Point& p): x(p.x), y(p.y) { }

        Point add(const Point& p) const;
        int taxiDistance() const;
        // etc... all const members

        const int x, y; // const can only be init'd at construction time

     private:
        Point& operator=(const Point& p); // never implemented!
}
贪了杯 2024-09-07 16:56:14

即使您能够做到这一点(我怀疑您做不到),请考虑其他人阅读您的代码。他们不太可能理解一切都是 const,因此也不太可能理解您的代码。

Even if you are able to do this (which I suspect you are not), think about other people reading your code. They are not likely to understand that everything is const and as a result are not likely to understand your code.

看透却不说透 2024-09-07 16:56:14

您可以保持代码标准 C++ 并设计额外的类型检查层。

空的 MUTABLE 宏可以作为 const 检查器的提示。在某些地方可能仍然需要显式 const 来使代码编译。

You could keep the code standard C++ and devise an extra layer of type checking.

An empty MUTABLE macro could serve as an hint for the const checker. Explicit const may still be needed at places to make the code compile.

伴我老 2024-09-07 16:56:14

我建议反对这一点。如果您设法实现了目标,那么任何人(包括一段时间后的您)都会在阅读您的代码并且其行为与他预期的不同时感到惊讶。

请在需要的地方添加对每个人都可见的 const 修饰符。您的代码将被更频繁地读取,而不是被写入!

I would advice against this. If you manage to achieve you goal anyone (including you after some time) is surprised when he reads your code and it behaves different than he expects.

Please add the const modifier plain visible for everyone wherever it's needed. Your code is going to be read fare more often then it's going to be written!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文