views:

186

answers:

4

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);
+1  A: 

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!
}
MtnViewMark
You got it wrong. He wants to stop writing the word "const" in all those places, and yet have C++ act like the word is there. useless exercise, of course. Anyway I think what he wants is impossible.
Stefan Monov
I answered what I think might be what his true aim might be, since simply trying to avoid typing const is inadvisable and only begs the question why? I've left a comment on the OP asking for clarification.
MtnViewMark
@Stefan: Yes, that's the point. Of course it's useless. I probably should have made it clear from the start that I understand what I'm doing and that I'm simply curious.
Jon Purdy
+2  A: 

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.

Mike Kale
This is a very negative attitude. Research in programming languages is an entirely valid field of study. If you want to make an incremental change to an existing language, you *have to* do something non-standard. If everyone took your attitude, no programming language would change ever.An immutable dialect of C++ might be just the right thing for some problems. I suspect not. But I applaud the poster for experimenting and learning.
Right. The OP's edit wasn't there when I submitted my answer. I didn't realize this question was about learning/ exploring the limits of C++ and/or non-standard extensions / programming techniques. I'm all for experimentation and research, just wanted to emphasize the importance of readable code for the next person to come along. Thanks! -Mike
Mike Kale
Cool! We're all in agreement then.
@user207442: Note that this answer was made before the question was made clear. It's really appropriately negative in that context. Your points, however, are still quite correct.
Jon Purdy
A: 

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!

Tobias Langner
+2  A: 

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.

stusmith
+1 This is definitely the best answer so far, but I'm going to hold off on accepting it to give other answers a chance to catch up to the edited question.
Jon Purdy