What is the simplest and least obtrusive way to indicate to the compiler, whether by means of compiler options, #define
s, typedef
s, 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);
By default, an integer constant is of type int .
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With