Although it's been a long time since I coded in C/C++, correct me if I'm wrong.
In the C++ Working Paper, the Standardese is N4527 5.2.2 [expr.call]/4 "When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument." followed by 8.5 [dcl.init] where /17.8 has a nice example:
"Note: An expression of type "cv1 T" can initialize an object of type "cv2 T" independently of the cv-qualifiers cv1 and cv2. int a; const int b = a; int c = b;"
(A "cv-qualifier" is const or volatile; they behave similarly in many contexts, hence the collective terminology for convenience.)
Assuming you do it from the start you shouldn't be running in to issues.
But from my experience const really helps in enforcing directed graph constraints on logic and data flow which is great - and forces code to be easier to reason about.
What's more, it can "infect" any other code that touches it, in turn requiring casting or weird const additions. For a simple rant, check the first few paragraphs here:
You can't just declare something a constant then cast it out of being constant later? Of course not, that's the entire point!
Or any of several other things, including some truly odd possibilities using operator and function overloading.
It's just really gross as implemented.
And this is 'const''s fault? Others have asked - can you provide an example? Because what you're describing sounds like it's going to be a hot mess, const or no const.
I don't really think that const can make bad data representation any worse than it already is.
std::vector<char*> vec1(...);
std::vector<char const*> vec2(...);
std::copy(vec1.begin(), vec1.end(), vec2.begin()); // works (1)
std::copy(vec2.begin(), vec2.end(), vec1.begin()); // fails (2)
If this compiled, the second example would invoke undefined behavior and crash if you attempted to write to an element that pointed to read-only memory (e.g., a string literal).Container element types are almost never declared const unless their values are truly immutable (in this case the pointer is mutable, but the underlying memory is not, which is kind of strange).
...
std::vector<char*> vec1(...);
foo(vec1); // fails
The idea is that you should be allowed to pass non-const vector to const function who accepts same vector (const or not const) with const variables. Because the function foo is like saying "give me vector, I will not modify it's elements", and with current C++ standard this is not allowed.If you're writing a program, you should use the same type, std::vector<char* >, everywhere.
If you're writing a library, the API should avoid including container details in function parameters, and const and non-const T should be allowed.
template<typename Container>
void foo1(Container const& c);
template<typename Iterator>
void foo2(Iterator first);
template<typename Iterator>
void foo3(Iterator first, Iterator last);
template<typename Iterator>
void foo4(Iterator first, std::size_t num);
template<typename T>
void foo5(std::vector<T> const& vec);
void foo6(char const* const* first, std::size_t num);
foo6 is a pretty good alternative here, imo: std::vector<char*> vec(...);
foo6(&vec.front(), vec.size());
It would be cool if the compiler could figure templated type conversion out, but the crux of the issue is, as I understand it, that T<A> and T<A const> are not necessarily implemented the same way.Also note that std::vector<std::string const> won't compile -- it wouldn't be copy-assignable -- so these issues mostly seem to arise specifically when dealing with C strings and/or C compatibility. And reinterpret_cast is amazingly helpful for porting from C.
void blah(vector<const char * > &xs) {
xs.push_back("blah");
}
And you have the code calling it: vector<char * > xs;
blah(xs);
And that would not be valid. The rationale for the conversion rules for pointers to pointers is based on a similar sort of problem. (In fact I think it's exactly the same problem, at any level, but that only just occurred to me, so perhaps not.)(This isn't to say there aren't other similar sorts of conversion that would be safe, and that the language perhaps should support, just that this isn't one of the valid ones.)
I'm not even sure I understand the rant, though I've been a fan of const for years so it's probably automatic by now. So, say you have a class with a const member, and you have to pass it in to a function? Well, that's a bit unusual - const members are pretty rare in my experience. But the underlying problem sounds like: you have a const object, and you want to pass it into a function. There are three possibilities, I suppose:
1. The function takes const T & or const T * , because it doesn't change that argument. So no problem.
2. The function takes T, because it doesn't change that argument (and the object is small and/or the programmer doesn't care about the copying - irrelevant issues here). So no problem.
3. The function takes T & or T * , because it changes that argument. Compilation fails! But - no problem! Because this is what you want. You passed what's supposedly a const object into a function that expected to be able to change it. Obviously compilation has to fail.
So maybe the problem is people declaring parameters as T & or T * , when they meant const T & or const T * ?
I bet if C++ had const by default, this problem would never arise. We'd be striding, right now, arm in arm, towards our glorious const-filled destiny, singing a song about it as we went.
(References, by contrast, can never be reseated. So const T &const doesn't make sense. But... I didn't try this right now to double check, so if it works - though I think it doesn't - don't blame me. Or, if you do, at least allow me to plead extenuating circumstances, in the form of C++.)