문제

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);
도움이 되었습니까?

해결책

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.

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!
}

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!

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top