2

Let's say I have following type:

struct XY
{
  short nIndex;
  
  constexpr operator int()
  {
     return nIndex;
  }
};

What can I change to make the following work:

char arr[10][255];

constexpr XY xy{1};

char *x = arr[xy];

I thought that just giving XY a convert-to-int operator would be enough but I'm getting the error:

no match for 'operator[]' (operand types are 'const char [10][255]' and 'const XY')

1
  • 4
    Sorry, but I couldn't resist: Is this a case of The XY Problem? 😊‎ Commented Dec 20, 2020 at 21:37

1 Answer 1

4

Since the variable is constexpr, you cannot call the conversion operator that is not const-qualified. Solution is simple: Make the conversion operator const qualified:

constexpr operator int() const
Sign up to request clarification or add additional context in comments.

1 Comment

Didn't expect the problem to be solved as simply. It worked. Thanks.

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.