0

I am trying to disable using some member functions with temporary objects. To achieve this, I declare a r-value reference overload as deleted:

struct X {};

struct A
{
  A();
  void f(X const& x);
  void f(X&&) = delete;
};

int main()
{
    A a;
    X x;

    a.f(X());
    a.f(x);

    return 0;
}

This works as expected giving a compiler error, but when I try this with constructors, it does not generate a compile-error:

struct X {};

struct A
{
  A(X const&);
  A(X&&) = delete;
};

int main()
{
    X x;

    A b(X()); // using deleted constructor
    A c(x);

    return 0;
}

Why is this compiling? Is the A(X const&) constructor a better match than the A(X&&) constructor?

Jens
  • 9,058
  • 2
  • 26
  • 43
  • 1
    What happens if you replace `A b(X())` with `A b{X{}}`? [Most vexing parse](http://en.wikipedia.org/wiki/Most_vexing_parse)? See also http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work – nosid Mar 23 '14 at 10:20
  • Thanks, it is most vexing parse. This is something I definitely would change in the grammar and break backwards compatibility. – Jens Mar 23 '14 at 10:45

1 Answers1

0

Most vexing parse:

struct X {};

struct A
{
  A(X const&);
  A(X&&) = delete;
};

int main()
{
    X x;

    A b(X()); // using deleted constructor
} 

A b(X()) does not call the constructor but declares a function (Most vexing parse: why doesn't A a(()); work?).

Community
  • 1
  • 1
Jens
  • 9,058
  • 2
  • 26
  • 43