1

I've faced need to pass non-const static array to const argument. As I've discovered, const_cast may be used not only to remove, but also to add constness to type. So here's over-simplified version of what I'm trying to do:

int a[3] = { 1, 2, 3 };
const int b[3] = const_cast<const int[3]&>( a );

However it seems that compiler is unable to parse this with errors like

5:43: error: expected '>' before '&' token
5:43: error: expected '(' before '&' token
5:44: error: expected primary-expression before '>' token
5:50: error: expected ')' before ';' token

I've also tried to cast using pointers but got the same mistakes. Besides I don't want to switch to pointers as it would require to update quite big chunk of code.

It seems like relatively easy task, yet I'm already stuck on this for some time and wasn't able to find any useful info even remotely related to this topic.

UPD:

Thanks to comments I've found out that root cause in my case was not related to const_cast. If anyone is interested I was trying to initialise vector with list of static arrays of different sizes which apparently is not possible.

However since it was unobvious syntax of reference to array that led me to ask a question, I'm going to accept answer that explains it.

5
  • 1
    There is no need to do this. You can pass non-const arguments to const parameters directly. Please show a minimal reproducible example of what you were trying to do and what error you got. Commented Jan 17, 2020 at 17:26
  • I'd rather post the function and the parameter you want to pass that misses constness. I wouldn't be surprised if you do not need a cosnt_cast Commented Jan 17, 2020 at 17:27
  • It is particularly unclear right now whether your intention is to make a const copy of your array or whether you intend to form a const reference to the original array. Please clarify by providing a demonstration of the code that initially failed. Commented Jan 17, 2020 at 17:40
  • @walnut @formerlyknownas_463035818 you're right. I've tried to prepare the example of initial issue and faced another errors that have nothing to do with const_cast. Could you please advise me if I should change this question or create another one for the initial issue? Commented Jan 17, 2020 at 17:41
  • @Dheinamar If the error is related to the parameter's constness then I think adding to this question would be ok. If it is not, then I would recommend opening a new question, since changing the subject of the question would invalidate the answers that are already given. In either case make sure though that you post a complete minimal reproducible example and all error messages it generates verbatim as well as an explanation of what you originally intended to do. Also don't overwrite the code and error messages currently in this question, since that would invalidate answers as well. Commented Jan 17, 2020 at 17:58

2 Answers 2

3

Firstly, your syntax for reference-to-array is wrong. The correct form is const int (&)[3]. Secondly, an array cannot be initialised from another array. Thirdly, it is typically unnecessary to cast non-const to const because such conversion is implicit.

Simplest way to make a const copy of an array is to use a wrapper class for the array. The standard library provides a template for such wrapper: std::array. Example:

std::array a { 1, 2, 3 };
const std::array b = a;
Sign up to request clarification or add additional context in comments.

2 Comments

If at all, I think it is more likely that OP is looking for std::as_const rather than making a const copy. (Altough I am more inclined to believe that this is an XY problem to begin with.)
@walnut maybe. I wouldn't be able to guess that based on the question. To me, the code example looks like an attempt at making a const copy.
0

This is not the syntax of reference to arrays. It would be spelled like this:

int a[3] = {1, 2, 3};
const_cast<const int(&)[3]>(a);

But your array cannot be copied. You must have a reference to it or use std::array:

int a[3] = {1, 2, 3};
auto& b = const_cast<const int(&)[3]>(a);

// or use std::array

std::array a = {1, 2, 3};
auto const b = a;

1 Comment

Simpler to use const auto& b = a for the reference though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.