Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make All Types Constant by Default in C++

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);
like image 313
Jon Purdy Avatar asked May 04 '10 05:05

Jon Purdy


People also ask

What is the default type for a constant in C?

By default, an integer constant is of type int .

How do you declare constants in C?

The const keyword Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.

What is constant explain different types of constants available in C with examples?

In C language, a number or character or string of characters is called a constant. And it can be any data type. Constants are also called as literals. Primary constants − Integer, float, and character are called as Primary constants.

How to define constants in C programming?

using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword. Below program shows how to use const to declare constants of different data types: Refer Const Qualifier in C for details. This article is contributed by Chinmoy Lenka.

What is the default value of constant variables in C?

The default value of constant variables are zero. A program that demonstrates the declaration of constant variables in C using const keyword is given as follows. The output of the above program is as follows.

How to declare constant variables in C using const keyword?

A program that demonstrates the declaration of constant variables in C using const keyword is given as follows. The output of the above program is as follows. Variables can be declared as constants by using the #define preprocessor directive as it declares an alias for any value.

How many times can a constant variable be initialized in C?

The constant variables can be initialized once only. The default value of constant variables are zero. A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.


2 Answers

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.

like image 50
stusmith Avatar answered Sep 28 '22 13:09

stusmith


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.

like image 45
Mike Kale Avatar answered Sep 28 '22 12:09

Mike Kale