Sergii
Sergii

Reputation: 255

const in C vs const in C++

The given code compiles in C but fails in C++.

int main()
{
   const int x; /* uninitialized const compiles in C but fails in C++*/
}

What is the rationale and the reason behind the change from C to C++?

Upvotes: 15

Views: 6163

Answers (5)

pro coder
pro coder

Reputation: 1

#include<iostream>
using namespace std;

class Test
{
    int value;
    public:
        Test(int v = 0) {value = v;}
        int getvalue() const {return value;}
};

int main(){

    Test t(20);
    cout << t.getvalue();
    return 0;
     
    const double P1 = 68.68;
    cout<<P1<<endl;
    
    int star  = 57;
    int const *pstar = &star;
    cout<<*pstar<<endl;
    
    return 0;
    
}

Upvotes: -1

pankiii
pankiii

Reputation: 435

The ISO standard says (in 8.5 [dcl.init] paragraph 9):

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor.

if you try the same example after modifying to this:

int main()
{
   /*Unless explicitly declared extern, a const object does not have
 external linkage and must be initialized*/
   extern const int x; 
   return 0;
}

it will get compiled. So this self explains the need of enforcing this error to c++, declaring const vars without initializing and extern linkage is of no use, so coder must have added it by mistake.

Upvotes: 3

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

Note that there is a legitimate use of an uninitialized, const-qualified object of automatic storage duration: its address can be taken and used as a unique key for labeling recursion levels in a recursive function. This is somewhat obscure, but worth noting. C makes this use efficient, while C++ requires you waste time and code size on initializing it. (In theory the compiler could perhaps determine that the value is never used and optimize out the initialization, but since you're passing around a pointer, that would be rather difficult to prove.)

Upvotes: 13

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506877

See the spec, in the compatibility appendix C.1.6:

7.1.6 [see also 3.5]

Change: const objects must be initialized in C++ but can be left uninitialized in C

Rationale: A const object cannot be assigned to so it must be initialized to hold a useful value.

Effect on original feature: Deletion of semantically well-defined feature.

Difficulty of converting: Semantic transformation.

How widely used: Seldom.

Upvotes: 15

Dietrich Epp
Dietrich Epp

Reputation: 213298

The const keyword was introduced to C in C89 in 1989, but had been with C++ since its creation in 1983. So it was "backported" from C++ to C.

Initialization semantics are generally different in C and C++. Although most of the time they "just do the thing you expect", there are cases where the differences become quite important. C++ really isn't a superset of C after all.

For example, in C++ you can't:

goto x;
int i = 3;
x:
puts("Hello, world");

But that is perfectly legal in C.

Upvotes: 6

Related Questions