0

When I was using visual studio 2022, I noticed that its std::move function defines the [[msvc::intrinsic]] attribute as a macro. /std:c++latest produces some non-standard behavior

That is, the lifetime of the temporary object is extended, but in principle, the move function call has a return, can not be extended.

I tried the toggle option standard, but it was the same.

Here's a demo I wrote, replacing std::move with my own mmove, adding [[msvc::intrinsic]] attributes, and a normal control group.

I want to know what this property does and why msvc is designed this way.

#include<iostream>

struct A{
    ~A() { puts(__FUNCTION__); }
};

template <class Ty>//Added an attribute, behavior equivalent to std::move in /std:c++latest, non-standard, extended lifetime
[[msvc::intrinsic]] constexpr std::remove_reference_t<Ty>&& mmove(Ty&& Arg) noexcept {
    return static_cast<std::remove_reference_t<Ty>&&>(Arg);
}

template <class Ty>
constexpr std::remove_reference_t<Ty>&& nmove(Ty&& Arg) noexcept {
    return static_cast<std::remove_reference_t<Ty>&&>(Arg);
}


int main()
{
    {
        A&& rvalue = mmove(A{});
        puts(__FUNCTION__);
    }
    puts("------");
    {
        A&& rvalue = nmove(A{});
        puts(__FUNCTION__);
    }
}

enter image description here

I look forward to getting a detailed explanation of the '[[msvc::intrinsic]]' properties.

归故里
  • 9
  • 1
  • it's in documentation, https://github.com/MicrosoftDocs/cpp-docs/blob/main/docs/cpp/attributes.md detailed enough, what else you want to know? – Swift - Friday Pie Jul 25 '23 at 07:22
  • Thank you very much for your reply. I understand after checking the document! – 归故里 Jul 25 '23 at 07:39
  • you may want to report the problem. It's a regression, older versions didn't show this behaviour. Implicit "inlining" of function's code in this case emulates a life-time extension via double referencing, which `std::move` shouldn't have done. Even if that solves some minor edge cases. – Swift - Friday Pie Jul 25 '23 at 07:45
  • As far as I know, in fact, 'static_cast(T{}) 'This form is also required by the standard to return dangling references, not extend the lifetime of temporary objects, but currently gcc msvc clang is implemented this way, I also heard that the current standard has a flaw in this, you know? – 归故里 Jul 26 '23 at 03:03
  • Not sure if its considered a flaw. `std::move` cannot be properly implemented like that by strict standard, what MS compiler does is a "hack". GCC uses and intrinstic function afaik (and those are unaffected by language rules). IT's somewhat unrelated to the idea that `[[msvc::intrinsic]]` breaches as-if rule. It's an option that should be used sparingly. – Swift - Friday Pie Jul 28 '23 at 06:54

0 Answers0