andreasdr
andreasdr

Reputation: 4054

What does the input argument type "const double *&" mean?

Consider the following code:

void func1(const double& x) {
    x = 1.2; //compilation error! (assignment of read-only reference)
}
void func2(const double* x) {
    *x = 1.3; //compilation error! (assignment of read-only location)
}
void func3(const double*& x) {
    *x = 1.4; //compilation error! (assignment of read-only location)
}
int main()
{
    double a = 1.1;
    func1(a);
    double *y = &a;
    func2(y);
    func3(y); //compilation error! (invalid initialization of reference of type 'const double*&' from expression of type 'double*')
    return 0;
}

Here, my understanding is that func1() takes a reference to a double, and that func2() takes a pointer to a double. In both cases, the const keyword means that the value of the double can't be modified by the function. Hence, I get compilation errors as expected.

But what's going on with func3()? I can't seem to find an explanation for what this input argument type means that would explain the compilation errors. I would assume that const double*& x means "a reference to a double*, which can't be modified". But this seems to be the wrong interpretation: I can't send in a double* (but I CAN send in a const double*) and I can modify the address that the pointer points to (but I CAN'T modify the double that the pointer points to).

I guess my confusion comes from a misunderstanding of what the const keyword is "attached" to: to the &? to the double*? What is the interpretation of const double*& x that would clear up my confusion?

EDIT: This question is distinct from the suggested duplicate because it in large part has to do with how to interpret what the const keyword "attaches to".

Upvotes: 2

Views: 3198

Answers (3)

shrike
shrike

Reputation: 4501

const double*& is a reference to a pointer to a const double. So *x is a const double, which you cannot assign thus. const is attached to double here.

You may want to use either double* const (which is a const pointer to [non-const] double) or double* const& (which is a reference to a const pointer to [non-const] double).

Upvotes: 4

songyuanyao
songyuanyao

Reputation: 172894

const double*& x, means a non-const reference to const double*.

When you pass a double*, it need to be casted to const double*, which is a temporary variable and can't be bound to a non-const reference, that's why compiler complains.

BTW: const double* const & x is a const reference to const double*.

Upvotes: 2

ad3angel1s
ad3angel1s

Reputation: 500

const double*& x

means that x is a reference to a pointer to a const double, which implies the object can't be modified (i.e. const is a type qualifier attached to double, not the pointer object itself in this case)

Upvotes: 3

Related Questions