【问题标题】:Initialise const member with make_shared用 make_shared 初始化 const 成员
【发布时间】:2015-10-09 19:53:16
【问题描述】:

以下代码使用 Xcode 6.3.2 而不是 Visual Studio 2013 编译。

#include <cstdint>
#include <memory>

class Y
{
public:
    Y(uint32_t i) : m_i(i)
    {
    }

private:
    uint32_t m_i;
};

class X
{
public:
    X() = default;

private:
    const uint32_t m_dimension = 2;
    const std::shared_ptr<Y> m_y = std::make_shared<Y>(m_dimension);
};

报错是:

error C2783: 'std::shared_ptr<_Ty> std::make_shared(_Types &&...)' : could not deduce template argument for '_Ty'

这可能是 Visual Studio 中的编译器错误还是代码有问题?

【问题讨论】:

  • 此代码在 Visual 2013(更新 4)上编译没有任何错误。要生成此错误,我必须写 make_shared( 而不是 make_shared&lt;Y&gt;(
  • 这个bug好像是documented
  • @Hcorg - 更新 4?我如何找到这些信息?我只看到:Microsoft Visual Studio Express 2013 for Windows Desktop Version 12.0.21005.1 REL Microsoft .NET Framework Version 4.5.50938
  • @ksl 我的 2013 更新 4 显示 12.0.31101.00 更新 4。但从今天开始,您可以直接跳到 2015!
  • @Alejandro 谢谢。你想把它作为答案,我会接受吗?

标签: c++ c++11 visual-studio-2013 shared-ptr make-shared


【解决方案1】:

看来你不是唯一一个遇到这个问题的人——这个错误是documented!较新版本的 Visual Studio 不应该有这个问题(Visual Studio 2013 的更新 4 似乎是起点)。

【讨论】:

    【解决方案2】:

    我在使用 VS2013 更新 4 时遇到了同样的问题,并通过将其更新到版本 5 解决了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多