Why can't I declare a variable with auto?

I get a compilation error in Visual Studio 2015 when I try to declare a class variable when these classes use the PIMPL template.

foo.h:

#pragma once

class Foo
{
public:
  Foo(const std::wstring& str,
      const std::vector<std::wstring>& items);
  ~Foo();

private:
  struct Impl;
  std::unique_ptr<Impl> pimpl;
};

foo.cpp:

#include "stdafx.h"
#include "Foo.h"

struct Foo::Impl
{
public:
  Impl(const std::wstring& str,
       const std::vector<std::wstring>& items);

  std::wstring str_;
  std::vector<std::wstring> items_;
};

Foo::Foo(const std::wstring& str,
         const std::vector<std::wstring>& items)
  : pimpl(std::make_unique<Impl>(str, items))
{
}

Foo::~Foo() = default;

Foo::Impl::Impl(const std::wstring& str,
                const std::vector<std::wstring>& items)
  : str_(str),
  items_(items)
{
}

If I declare a type variable Foousing the traditional syntax, it compiles fine:

  Foo f(L"Hello", std::vector<std::wstring>());

However, if I declare it using auto, I get a compilation error:

  auto f2 = Foo { L"Goodbye", std::vector<std::wstring>() };

error C2280: 'Foo :: Foo (const Foo &)': attempt to reference a remote function
note: the compiler generated 'Foo :: Foo' here

, Foo , unique_ptr . , , , .

Visual Studio 2013. Visual Studio 2015, , , .

- , - ?

+4
2

, (. [class.copy]/(9.4)). , unique_ptr .

.

+10

std::unique_ptr, -, , ( -) .

, default.

+5

Source: https://habr.com/ru/post/1624811/


All Articles