3

AFAIK, if you cast to non-reference type, you get an prvalue.

int x = 234;
(int)x = 23;

std::cout << x << "\n"; 

Output : 23 (in msvc)

In GCC and Clang, cstyle cast return a prvalue (as expected), meanwhile in MSVC, it return an lvalue.

Am i missing something or it is an bug in MSVC ?

see live demo here

mn_op
  • 107
  • 6
  • 1
    I think MSVC is just ignoring the redundant cast, casting to a different type produces the error you are expecting: https://godbolt.org/z/xfW5jndfP – Alan Birtles Oct 16 '22 at 17:27
  • 2
    You [forgot something](https://godbolt.org/z/P4z8eaqPz). – n. m. could be an AI Oct 16 '22 at 17:34
  • 1
    When set to C++ versions before C++20 MSVC deviates in some aspects from the language standard by-default. If you want to have it behave (more) standard-conforming either use C++20 or later or set `/permissive-` (standard conformance mode). – user17732522 Oct 16 '22 at 18:37

1 Answers1

3

If you cast to an lvalue reference type you will get a lvalue. If you cast to rvalue reference type you will get a xvalue. If you cast to non-reference type you get an prvalue. So, you should get a prvalue here.

Use the latest version of c++ in msvc, you will get your expected output.

Use flag : /std:c++latest

Jitu DeRaps
  • 144
  • 1
  • 9