0

While experimenting with the behaviour of deleted constructors with a wrapper template, I stumbled upon this curious issue:

#include <utility>

template <typename T>
struct Foo
{
    T t;
    Foo(const T& t) : t(t) { }
    Foo(T&& t) : t(std::move(t)) { }
};

struct Bar
{
    Bar() { }
    Bar(int) { }
    Bar(const Bar&) = default;
    Bar(Bar&&) = delete;
};

int main()
{
    Foo<Bar> foobar1(Bar()); //compiles fine, using the Bar copy constructor
    Foo<Bar> foobar2(Bar(5)); //error: use of deleted function 'Bar::Bar(Bar&&)

    return 0;
}

I can see why it would opt for the move constructor, but why does it behave differently when I use Bar() or Bar(5)? Is this intended, undefined or bugged behaviour?

I tested this at ideone.com with C++14, they use gcc 4.9.2: https://ideone.com/9CS56F

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Aberrant
  • 3,423
  • 1
  • 27
  • 38

0 Answers0