samedi 2 avril 2016

clang and gcc vs. MSVC and icc: Is a static_assert in the copy/move constructor required to work if copy elision could apply, too?

I have a static_assert in a move constructor of a template struct of mine. Is this static_assert required to be considered by the compiler, even if copy elision is possible?

This is the stripped-down scenario:

template<typename T>
struct X
{
  X(const X&&)
  {
    static_assert(sizeof(T) < 1, "");
  }
};

auto impl() -> X<int>;

auto test() -> decltype(impl())
{
  return impl();
}

int main()
{
  test();
}

gcc and clang agree to evaluate the static_assert and fail to compile.

MSCV and icc on the other hand compile the code just fine.

Interestingly enough, when I remove the definition of the move constructor like this:

template<typename T>
struct X
{
  X(const X&&);
};

gcc and clang also compile the code now. Thus, all compilers seem to agree that the definition of the move constructor is irrelevant for copy elision.

The question therefore is: If there is a static_assert in the copy/move constructor, does the standard require it to be evaluated even if copy/move elision is possible?

Aucun commentaire:

Enregistrer un commentaire