Search code examples
c++c++11visual-studio-2013shared-ptrmake-shared

Initialise const member with make_shared


The following code compiles using Xcode 6.3.2 but not 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);
};

The error reported is:

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

Could this be a compiler bug in Visual Studio or is there something wrong with the code?


Solution

  • It appears as though you're not the only one with this issue -- this bug has been documented! Newer versions of Visual Studio shouldn't have this issue (Update 4 of Visual Studio 2013 seems to be the starting point).