Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c++ - Workaround for error C2536: cannot specify explicit initializer for arrays in Visual Studio 2013

the following code does not compile with Visual Studio 2013 while it should:

class A
{
    A() :m_array{ 0, 1, 2 } {} // error C2536: 'A::A::m_array' : cannot specify explicit initializer for arrays
private:
    int m_array[3];
};

See bug report for more details.

What are the possible workarounds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As the comments, you can try this workaround.

class A
{
    A() : m_array ({ 0, 1, 2 }) {}
private:
    std::array<int, 3> m_array;
};

It seems VS2013 made initializer-list for std::array constructor well and you can initialize it in constructor's intializer. The code that you wrote is valid and both gcc and clang support it. VS2013 lacks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...